【问题标题】:iPhone - Populating an array with the content of compressed plist fileiPhone - 使用压缩 plist 文件的内容填充数组
【发布时间】:2011-09-19 16:00:34
【问题描述】:
假设我有一个 plist 文件,我想压缩它。我有一个方法可以加载这个压缩文件,解压缩它,然后把结果放到一个 NSString 中。
当 plist 未压缩时,如何将 NSString 转换为一个数组,就像使用这些行一样简单:
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];
NSArray* arrayOfDatas = [NSArray arrayWithContentsOfFile:filePath];
【问题讨论】:
标签:
iphone
ios
arrays
compression
plist
【解决方案1】:
将文件读入NSData 然后使用:
+ (id)propertyListWithData:(NSData *)data options:(NSPropertyListReadOptions)opt format:(NSPropertyListFormat *)format error:(NSError **)error
其中NSPropertyListFormat 是NSPropertyListBinaryFormat_v1_0
创建到NSData 写出来是:
+ (NSData *)dataWithPropertyList:(id)plist format:(NSPropertyListFormat)format options:(NSPropertyListWriteOptions)opt error:(NSError **)error
示例(未测试):
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSError *error;
NSArray* plist = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:&error];
NSData *propertyListSerializedData = [NSPropertyListSerialization dataWithPropertyList:plist format:NSPropertyListBinaryFormat_v1_0 options:0 error:&error];
[propertyListSerializedData writeToFile:filePath atomically:YES];