Foundation 框架提供了 NSPropertyListSerializat
//从文件读入数据到NSMutableArray
- (NSMutableArray *) readFromArray:(NSString *) path
{
NSMutableData *data = [[NSMutableData alloc] initWithContentsOfFile:path];
NSMutableArray *array = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListMutableContainersAndLeaves format:NULL error:NULL];
return array;
}
//写入NSMutableArray 到文件
- (void) write:(NSMutableArray *) array toFilePath:(NSString *) path
{
NSData *data = [NSPropertyListSerialization dataWithPropertyList:array format:NSPropertyListBinaryFormat_v1_0 options:NSPropertyListMutableContainersAndLeaves error:NULL];
BOOL success = [data writeToFile:path atomically:YES];
if (success == NO)
{
NSLog(@"写入不成功");
}
}
options
NSPropertyListImmutable 属性列表包含不可变对象
NSPropertyListMutableContainers 属性列表父节点是可变的类型,子节点是不可变类型
NSPropertyListMutableContainersAndLeaves 属性列表父节点和子节点都是可变的类型
format:
NSPropertyListXMLFormat_v1_0 指定属性列表文件格式为XML格式,仍然是纯文本类型,不会压缩文件
NSPropertyListBinaryFormat_v1_0 指定属性列表文件格式为二进制格式,文件是二进制类型,会压缩文件
NSPropertyListOpenStepFormat 指定属性列表文件格式为ASCII码格式,对于旧格式的属性列表文件,不支持写入操作