2.plist存储

Posted on 2016-07-09 22:59  柠檬片  阅读(81)  评论(0)    收藏  举报
 1 // 存储
 2 - (IBAction)save:(id)sender {
 3     // Plist存储
 4     // 只有iOS中才有plist
 5     
 6     // plist存储本质,就是帮你生成一个Plist文件
 7     // 谁才能做Plist,(数组,字典)
 8     // plist文件注意点:plist文件不能存储自定义对象
 9     
10     Person *p = [[Person alloc] init];
11     
12     NSArray *arr = @[@"123",@1];
13     
14     // File:文件的全路径
15     // 1.先明确文件存储到哪,应用沙盒的某个文件夹中
16     
17     // 获取应用沙盒路径
18 //    NSString *homePath = NSHomeDirectory();
19 //    NSLog(@"%@",homePath);
20     
21     // 获取Caches文件夹路径
22     // directory:搜索文件夹
23     // domainMask:在哪个范围内搜索 NSUserDomainMask:在用户中查找
24     // expandTilde: YES :在路径展开 NO:不展开路径 ~:代替沙盒路径
25     NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
26     
27     // 拼接文件名
28     NSString *filePath = [cachesPath stringByAppendingPathComponent:@"arr.plist"];
29     
30     
31     [arr writeToFile:filePath atomically:YES];
32     
33 }
存储
 1 // 读取
 2 - (IBAction)read:(id)sender {
 3     NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
 4     
 5     // 拼接文件名
 6     NSString *filePath = [cachesPath stringByAppendingPathComponent:@"arr.plist"];
 7     
 8     // 读取:以什么形式存储就以什么形式读取
 9     NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];
10     
11     NSLog(@"%@",arr);
12     
13 }
读取