您的位置:首页 > 其它

plist文件的归档,解档

2016-05-20 18:18 351 查看
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
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: