提到归档, 不得不说一句iOS数据持久化方式:
1.属性列表(Plist文件) ,NSUserDefaul
2.归档 (NSKeyedArchiver)
3.数据库(SQlite, CoreData, FMDB(第三方))
归档: 将复杂对象转化为NSData类型数据 (复杂 ---> 归档 --->NSData-->writeToFile写入文件)
注意: 归档是将对象转化为数据字节, 以文件的形式存储在磁盘上.
反归档: 将NSData类型对象转化为复杂对象(读取文件--->转化成NSData文件--->反归档--->复杂对象)
这里介绍对自定义类的归档反归档方法
自定义Person类:
注意事项: 一个类要想实现归档反归档, 就必须遵循NSCoding协议, 实现两个方法, 下面详细介绍
#import <Foundation/Foundation.h>
@interface Person : NSObject<NSCoding> //* 遵循NSCoding 协议
//类的属性
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *gender;
@property (assign, nonatomic) NSInteger age;
//* 遵循协议后需要实现的方法
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (instancetype)initWithCoder:(NSCoder *)aDecoder;
@end
#import "Person.h"
@implementation Person
//归档(序列化)
//对Person类对象进行归档时, 执行此方法
//并对Person类中想要进行归档的所有属性, 进行序列化操作
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeInteger:_age forKey:@"age"];
[aCoder encodeObject:_gender forKey:@"gender"];
}
//反归档(反序列化)
//对对Person类对象进行反归档时, 执行此方法
//并对Person类中想要进行反归档的所有属性, 进行反序列化操作
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]) {
_name = [aDecoder decodeObjectForKey:@"name"];
_gender = [aDecoder decodeObjectForKey:@"gender"];
_age = [aDecoder decodeIntegerForKey:@"age"];
}
return self;
}
@end
归档过程
在Controller 里面调用该归档类, 初始化归档对象, 进行归档反归档操作
------- 对自定义对象进行归档和反归档 --------
- (void)archive{
Person *per = [Person new];
per.name = @"姓名";
per.age = 10;
per.gender = @"男";
//归档
//1. 准备路径
NSString *path = NSHomeDirectory();
path = [path stringByAppendingString:@"SingeDog.txt"];
//2. 准备存储数据的对象(用可变数据类型来接收)(用来初始化归档工具)
NSMutableData *data = [NSMutableData data];
//3. 创建归档对象
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
//4.开始归档
[archiver encodeObject:per forKey:@"person"];
//5. 完成归档
[archiver finishEncoding];
//6. 写入文件
BOOL result = [data writeToFile:path atomically:YES];
if (result) {
NSLog(@"归档成功: %@",path);
}
//反归档
//1. 创建对象, 获取接档路径
NSData *myData = [NSData dataWithContentsOfFile:path];
//2. 创建反归档对象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:myData];
//3. 反归档(创建对象, 用来接收反归档的结果)
Person *aper = [Person new];
//key: 必须与归档时相同
aper = [unarchiver decodeObjectForKey:@"person"];
//4. 结束反归档
[unarchiver finishDecoding];
NSLog(@"%@", aper.name);
}```
------- 对Foundation框架中的对象进行归档和反归档 --------
oc中的基本NS数据都可以直接进行归档, 反归档操作
-
(void)foundationClass{
//归档(以数组为例说明)
//1. 创建路径
NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];//2. 拼接一个文件
NSString *filePath = [document stringByAppendingString:@"/file.plist"];//3. 创建归档对象
NSArray *array = @[@"C", @"OC", @"UI"];//4. 归档
//archiveRootObject: 对基本数据类型数据进行归档的方法
BOOL result = [NSKeyedArchiver archiveRootObject:array toFile:filePath];if (result) {
NSLog(@"array归档成功:%@", filePath);
}
//5. 不需要结束
//反归档
//1. 创建对象用来接收
NSArray *newArray = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"解档结果 = %@", newArray);
}```
------- 对自定义内容进行归档和反归档 --------
- (void)customContent{
//归档
//1. 获取路径
NSString *custonPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
custonPath = [custonPath stringByAppendingString:@"/customFile"];
//2. 使用Data对象存放归档数据
NSMutableData *data = [NSMutableData data];
//3. 创建归档对象
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
//4. 对自定义内容进行归档
[archiver encodeObject:@"尼古拉斯.赵四" forKey:@"name"];
[archiver encodeObject:@"男" forKey:@"gender"];
[archiver encodeInt:22 forKey:@"age"];
[archiver encodeObject:@[@"AAAA", @"BBBB", @"CCCC"] forKey:@"array"];
//5. 完成归档
[archiver finishEncoding];
//6. 写入本地
BOOL result = [data writeToFile:custonPath atomically:YES];
if (result) {
NSLog(@"写入成功: %@", custonPath);
}
//反归档:
//1. 读取文件, 转化成NSData对象类型
NSData *unarchiverData = [NSData dataWithContentsOfFile:custonPath];
//2. 创建反归档对象
NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:unarchiverData];
//3. 反归档(根据KEY值访问)
NSString *str = [unArchiver decodeObjectForKey:@"name"];
NSLog(@"%@", str);
}```