1:对foundation框架的对象进行归档
2:对自定义的对象进行归档
3:对自定义内容进行归档
注意:归档生成的文件是加密的
与属性列表相反,同样作为轻量级存储的持久化方案,数据归档是进行加密处理的,数据在经过归档处理会转换成二进制数据,所以安全性要远远高于属性列表
http://www.jianshu.com/p/bc76405adf95
1.自己写的demo 把对象归档成NSData然后存到NSUserDefaults中
https://gitee.com/623128690/codes/zofjqvu1atdm9in4rks3b54#3.pch.h
2.把归档存到沙盒里面去
http://www.mamicode.com/info-detail-1028111.html
//viewController.m文件中
//获取沙盒中Documents文件夹路径
NSString *documents =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
NSString * documentPath =[documents stringByAppendingPathComponent:@"documentPath.person"];
//归档
Possession * person =[[Possession alloc]init];
person.name =@"wangbin";
person.age=22;
person.height=178.9;
[NSKeyedArchiver archiveRootObject:person toFile:documentPath];
//反归档
Possession *person1 =[NSKeyedUnarchiver unarchiveObjectWithFile:documentPath];
NSLog(@"name=%@,age=%lu,height=%.2f",person1.name,person1.age,person1.height);
NSLog(@"%@",documentPath);
3.两种不同的归档反归档
[NSKeyedUnarchiver unarchiveObjectWithFile:<#(nonnull NSString *)#>];
[NSKeyedUnarchiver unarchiveObjectWithData:<#(nonnull NSData *)#>];
[NSKeyedArchiver archiveRootObject:<#(nonnull id)#> toFile:<#(nonnull NSString *)#>]
[NSKeyedArchiver archivedDataWithRootObject:<#(nonnull id)#>];
网友评论