归档Archiver -> 对数据进行写操作
解档(反归档)Unarchiver - > 对数据进行读操作
总结:
1. 一般不会使用归档 / 解档的方式存储基本数据类型
2. 本质:基本数据类型 <--> NSData
Demo1_NSKeyArchiver
<span style="font-size:14px;"><span style="font-size:14px;">//需求:把NSArray数据使用归档/解档写入并读取
//准备写入的文件路径:/Documents/archiver(文件名)
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *filePath = [documentPath stringByAppendingPathComponent:@"archiver"];
//归档步骤
NSArray *array = @[@"Jack", @19, @[@"Java", @"Swift", @"Ruby"]];
//1.准备一个可变数据类型
NSMutableData *mutableData = [NSMutableData data];
//2.创建NSKeyedArchiver对象
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mutableData];
//3.对数据进行编码
[archiver encodeObject:array forKey:@"arrayKey"];
//4.执行完成编码
[archiver finishEncoding];
//5.写入文件
[mutableData writeToFile:filePath atomically:YES];
//归档变相执行如下步骤:NSArray转成NSData写入文件
//解档步骤
//1.从指定的文件读取数据(NSData从文件中读取)
NSData *readData = [NSData dataWithContentsOfFile:filePath];
//2.创建NSKeyedUnarchiver对象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:readData];
//3.对数据进行解码
NSArray *readArray = [unarchiver decodeObjectForKey:@"arrayKey"];
//4.执行完成解码
[unarchiver finishDecoding];
//5.验证数据
for (id obj in readArray) {
NSLog(@"%@", obj);
}
//解档变相执行的操作:NSData转成NSArray
</span></span>
Demo02_Custom
前提: 只要是遵循了NSCoding协议的类型
步骤:
1.需要遵守NSCoding协议
2.必须实现协议要求的两个方法(编码 / 解码)
3.针对调用(控制器)接续添加Demo01的几个步骤
<span style="font-size:14px;">NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *filePath = [documentPath stringByAppendingPathComponent:@"archiver"];
//准备自定义的模型对象
Student *stu = [Student new];
stu.name = @"Maggie";
stu.age = 20;
//归档
NSMutableData *mutableData = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mutableData];
[archiver encodeObject:stu forKey:@"stuKey"];
[archiver finishEncoding];
[mutableData writeToFile:filePath atomically:YES];
//解档
NSData *readData = [NSData dataWithContentsOfFile:filePath];;
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:readData];
Student *readStu = [unarchiver decodeObjectForKey:@"stuKey"];
[unarchiver finishDecoding];
//验证数据
NSLog(@"name:%@; age:%d", readStu.name, readStu.age);</span>
Student.h
<span style="font-size:14px;">@interface Student : NSObject<NSCoding>
/**姓名*/
@property (nonatomic, copy) NSString *name;
/**年龄*/
@property (nonatomic, assign) int age;
@end</span>
Student.m
<span style="font-size:14px;">@implementation Student
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.name forKey:@"nameKey"];
[aCoder encodeInt:self.age forKey:@"ageKey"];
}
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]) {
self.name = [aDecoder decodeObjectForKey:@"nameKey"];
self.age = [aDecoder decodeObjectForKey:@"ageKey"];
}
return self;
}
@end</span>
Demo03_Custom_UserDefault
步骤:
1. 需要遵循协议NSCoding
2. 实现两个方法
3. 需要执行stu -> NSData -> NSUserDefaults转换
<span style="font-size:14px;"><span style="font-size:14px;">//准备自定义的模型对象
Student *stu = [Student new];
stu.name = @"Maggie";
stu.age = 20;
//NSUserDefaults写
//把Student->NSData
NSData *writeData = [NSKeyedArchiver archivedDataWithRootObject:stu];
[[NSUserDefaults standardUserDefaults] setObject:writeData forKey:@"stuKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
//NSUserDefaults读
NSData *readData = [[NSUserDefaults standardUserDefaults] objectForKey:@"stuKey"];
//NSData -> Student
Student *readStu = [NSKeyedUnarchiver unarchiveObjectWithData:readData];
//验证数据
NSLog(@"name:%@; age:%d", readStu.name, readStu.age);</span></span>
student 类未变