1,plist

Plist注意:不能存储自定义对象

 Plist:数组和字典,
  如何判断一个对象能不能使用Plist,就看下有没有writeToFile

获取应用的文件夹(应用沙盒)

NSString* homePath=NSHomeDirectory();

获取temp

NSTemporaryDirectory();

获取Cache文件路径

NSSearchPathDirectory:搜索的目录

NSSearchPathDomainMask:搜索范围NSUserDomainMask表示在用户的手机上查找

expandTilde是否展开全路径,如果没有展开 应用沙盒的路径就~

存储一定要展开路径

获取文件存取路径,此处以cache为例

NSString* cachePath=NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)[0];

拼接文件名称

NSString* filePath=[cachePath stringByAppendingPathComponent:@"personArr.plist"];

File:文件全路径 存

[arr writeToFile:filePath atomically:YES]

 取

NSArray* arr=[NSArray arrayWithContentsOfFile:filePath];

 1 //
 2 //  ViewController.m
 3 //  05-PIist存储
 4 //
 5 //  Created by xiaomage on 15/6/13.
 6 //  Copyright (c) 2015年 xiaomage. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 #import "Person.h"
11 
12 @interface ViewController ()
13 
14 @end
15 
16 @implementation ViewController
17 // 点击存储的时候调用
18 - (IBAction)save:(id)sender {
19     Person *p = [[Person alloc] init];
20     // Plist注意:不能存储自定义对象
21     // Plist:数组和字典,
22     // 如何判断一个对象能不能使用Plist,就看下有没有writeToFile
23     NSArray *arr = @[@"123",@1,p];
24 
25     
26     // 获取应用的文件夹(应用沙盒)
27 //    NSString *homePath = NSHomeDirectory();
28     
29     // 获取temp
30 //    NSTemporaryDirectory();
31     
32     // 获取Cache文件路径
33     // NSSearchPathDirectory:搜索的目录
34     // NSSearchPathDomainMask:搜索范围 NSUserDomainMask:表示在用户的手机上查找
35     // expandTilde 是否展开全路径,如果没有展开,应用的沙盒路径就是~
36     // 存储一定要要展开路径
37     NSString *cachePaht = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
38     
39     
40     // 拼接文件名
41     NSString *filePath = [cachePaht stringByAppendingPathComponent:@"personArr.plist"];
42     
43     
44     
45     NSLog(@"%@",cachePaht);
46     
47     
48     // File:文件的全路径
49     [arr writeToFile:filePath atomically:YES];
50     
51 }
52 
53 - (IBAction)read:(id)sender {
54     
55     NSString *cachePaht = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
56     
57     
58     // 拼接文件名
59     NSString *filePath = [cachePaht stringByAppendingPathComponent:@"arr.plist"];
60     
61    NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];
62     
63     NSLog(@"%@",arr);
64     
65 }
66 
67 - (void)viewDidLoad {
68     [super viewDidLoad];
69     // Do any additional setup after loading the view, typically from a nib.
70 }
71 
72 - (void)didReceiveMemoryWarning {
73     [super didReceiveMemoryWarning];
74     // Dispose of any resources that can be recreated.
75 }
76 
77 @end
View Code

相关文章: