1 #import "ViewController.h"
2 #import "CZPerson.h"
3
4 @interface ViewController ()
5
6 @end
7
8 @implementation ViewController
9
10 - (void)viewDidLoad {
11 [super viewDidLoad];
12 // Do any additional setup after loading the view, typically from a nib.
13
14 //反归档
15 //1.获取documents路径
16 NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
17 //2.拼接文件名
18 NSString * fileName = [path stringByAppendingPathComponent:@"contact.app"];
19
20 //3.反归档读取数据
21 CZPerson * person = [NSKeyedUnarchiver unarchiveObjectWithFile:fileName];
22
23 NSLog(@"name === %@ phone == %@",person.name,person.phone);
24 }
25 - (void) test01
26 {
27 //创建联系人对象
28 CZPerson * person = [[CZPerson alloc] init];
29
30 //给Person属性赋值
31 person.name = @"JackMeng";
32 person.phone = @"10086";
33
34 //1.获取documents路径
35 NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
36
37 //2,拼接文件名
38 NSString * fileName = [path stringByAppendingPathComponent:@"contact"];
39
40 //通过归档的方式存储
41 [NSKeyedArchiver archiveRootObject:person toFile:fileName];
42
43 NSLog(@"%@",fileName);
44 }
45 - (void)didReceiveMemoryWarning {
46 [super didReceiveMemoryWarning];
47 // Dispose of any resources that can be recreated.
48 }
49
50 @end