1、NSPropertyListSerialization
Foundation框架提供了 NSPropertyListSerialization 类处理plist文件
list 对象指 NSString,NSArray,NSDictionary,NSDate,NSNumber等数据形式 NSPropertyListSerialization 的作用:
- list对象转换NSData
+ (nullable id)propertyListWithData:(NSData *)data
options:(NSPropertyListReadOptions)opt
format:(nullable NSPropertyListFormat *)format
error:(out NSError **)error
- NSData转换list对象
+ (nullable NSData *)dataWithPropertyList:(id)plist
format:(NSPropertyListFormat)format
options:(NSPropertyListWriteOptions)opt
error:(out NSError **)error
options选项类型:
typedef NS_OPTIONS(NSUInteger, NSPropertyListMutabilityOptions) {
NSPropertyListImmutable = kCFPropertyListImmutable,
NSPropertyListMutableContainers = kCFPropertyListMutableContainers,
NSPropertyListMutableContainersAndLeaves = kCFPropertyListMutableContainersAndLeaves
};
NSPropertyListImmutable 属性列表包含不可变对象 NSPropertyListMutableContainers 属性列表父节点是可变的类型,子节点是不可变类型 NSPropertyListMutableContainersAndLeaves 属性列表父节点和子节点都是可变的类型
format格式类型:
typedef NS_ENUM(NSUInteger, NSPropertyListFormat) {
NSPropertyListOpenStepFormat = kCFPropertyListOpenStepFormat,
NSPropertyListXMLFormat_v1_0 = kCFPropertyListXMLFormat_v1_0,
NSPropertyListBinaryFormat_v1_0 = kCFPropertyListBinaryFormat_v1_0
};
NSPropertyListXMLFormat_v1_0 指定属性列表文件格式为XML格式,仍然是纯文本类型,不会压缩文件 NSPropertyListBinaryFormat_v1_0 指定属性列表文件格式为二进制格式,文件是二进制类型,会压缩文件 NSPropertyListOpenStepFormat 指定属性列表文件格式为ASCII码格式,对于旧格式的属性列表文件,不支持写入操作
2、NSJSONSerialization
Foundation框架提供了 NSJSONSerialization 类对JSON进行序列化处理
转换成JSON的对象必须具有如下属性:
- 顶层对象必须是NSArray或者NSDictionary
- 所有的对象必须是NSString、NSNumber、NSArray、NSDictionary、NSNull
- 所有NSDictionary的key必须是NSString类型
- 数字对象不能是非数值或无穷(NSNumber)
NSJSONSerialization的作用:
检测Foundation对象能否合法转换为JSON
+ (BOOL)isValidJSONObject:(id)obj;
JSON数据对象转换为NSData
+ (nullable NSData *)dataWithJSONObject:(id)obj
options:(NSJSONWritingOptions)opt
error:(NSError **)error
NSData转换为JSON数据对象
+ (nullable id)JSONObjectWithData:(NSData *)data
options:(NSJSONReadingOptions)opt
error:(NSError **)error
options选项类型
typedef NS_OPTIONS(NSUInteger, NSJSONReadingOptions) {
NSJSONReadingMutableContainers = (1UL
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?