归档(序列化),把对象转为字节码,以文件的形式存储到磁盘上;程序运行过程中或者当再次打开程序的时候,可以通过解归档(反序列化)还原这些对象。只要遵循了NSCoding协议的对象都可以通过它实现序列化,由于绝大多数支持存储数据的Foundation和Cocoa Touch类都遵循了NSCoding协议,因此,对于大多数类来说,归档相对而言还是比较容易实现的。
- //获得沙盒路径
- NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
- //获取文件路径,由于归档后文件会加密,所以文件后缀可以任意取
- NSString *filePath = [documentPath stringByAppendingPathComponent:@"file.archiver"];
- //创建一个字典,用来归档
- NSDictionary *archiveDic = @{@"name":@"jack"};
- //调用NSKeyedArchiver的类方法archiveRootObject:toFile:将对象归档(返回一个布尔值)
- if([NSKeyedArchiver archiveRootObject:archiveDic toFile:filePath]){
- NSLog(@"archive success!");
- }
- //调用NSKeyedUnarchiver的类方法unarchiveObjectWithFile:将文件解档
- NSDictionary *unarchiveDic = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
- NSLog(@"%@", unarchiveDic);
- //获得沙盒路径
- NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
- //获取文件路径,由于归档后文件会加密,所以文件后缀可以任意取
- NSString *filePath = [documentPath stringByAppendingPathComponent:@"file.archiver"];
- //创建一个MutableData对象用于存放需要归档的数据
- NSMutableData *archiveData = [NSMutableData data];
- //创建一个NSKeyedArchiver类的对象archiver,用来对归档对象进行编码,编码完成才能进行归档
- NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:archiveData];
- [archiver encodeObject:@"jack" forKey:@"name"];
- [archiver encodeInt:20 forKey:@"age"];
- [archiver encodeFloat:72.5 forKey:@"weight"];
- //完成编码
- [archiver finishEncoding];
- //将archiveData对象写入文件,完成归档
- if ([archiveData writeToFile:filePath atomically:YES]) {
- NSLog(@"archive success!");
- }
- //创建NSData对象来接收解档文件
- NSData *unarchiveData = [NSData dataWithContentsOfFile:filePath];
- //创建一个NSKeyedUnarchiver对象unarchiver,用来对需要解档的对象进行解码,解码后就能还原原对象的数据类型
- NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:unarchiveData];
- NSString *name = [unarchiver decodeObjectForKey:@"name"];
- int age = [unarchiver decodeIntForKey:@"age"];
- float weight = [unarchiver decodeFloatForKey:@"weight"];
- NSLog(@"name = %@, age = %d, weight = %.2f", name, age, weight);
- @interface Person : NSObject <NSCoding>
- @property (copy, nonatomic) NSString *name;
- @property (copy, nonatomic) NSString *gender;
- @property (assign, nonatomic) int age;
- @end
- @implementation Person
- //实现NSCoding协议中的归档方法
- - (void)encodeWithCoder:(NSCoder *)aCoder {
- [aCoder encodeObject:self.name forKey:@"name"];
- [aCoder encodeObject:self.gender forKey:@"gender"];
- [aCoder encodeInt:self.age forKey:@"age"];
- }
- //实现NSCoding协议中的解档方法
- - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
- if (self = [super init]) {
- self.name = [aDecoder decodeObjectForKey:@"name"];
- self.gender = [aDecoder decodeObjectForKey:@"gender"];
- self.age = [aDecoder decodeIntForKey:@"age"];
- }
- return self;
- }
- @end
- //获得沙盒路径
- NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
- //获取文件路径,由于归档后文件会加密,所以文件后缀可以任意取
- NSString *filePath = [documentPath stringByAppendingPathComponent:@"file.archiver"];
- //自定义对象
- Person *person = [[Person alloc] init];
- person.name = @"jack";
- person.gender = @"male";
- person.age = 20;
- //归档
- if ([NSKeyedArchiver archiveRootObject:person toFile:filePath]) {
- NSLog(@"archive success!");
- }
- //解档,person2实例接收解档后的对象
- Person *person2 = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
- NSLog(@"name = %@, gender = %@, age = %d", person2.name, person2.gender, person2.age);