plist文件的归档,解档

plist文件

plist的根Type只能是字典(NSDictionary)或者是数组(NSArray)所以归档时我们只能将数组或字典保存到plist文件中,但是NSString也能通过归档保存到plist文件中同时它也可以通过stringWithContentsOfFile解档,它保存到plist中时Type是空的,Value是有值的!


plist文件的归档
NSArray *arr = [[NSArray alloc] initWithObjects:@"1", @"2", nil];
// NSDocumentDirectory 要查找的文件
// NSUserDomainMask 代表从用户文件夹下找
// 在iOS中,只有一个目录跟传入的参数匹配,所以这个集合里面只有一个元素
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *filePath = [path stringByAppendingPathComponent:@"xxx.plist"];
[arr writeToFile:filePath atomically:YES];
plist文件的解档
NSString *filePath = [path stringByAppendingPathComponent:@"xxx.plist"];
// 解档
NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];
NSLog(@"%@", arr);



文/MelodyZhy(简书作者)
原文链接:http://www.jianshu.com/p/cd475693e2f8
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
原文地址:https://www.cnblogs.com/W-Kr/p/5513104.html