归档也叫序列化,就是把对象、变量以一定的格式写入文件中进行存储,等到需要的时候直接读取出来,还原原数据的格式。ios中主要有以下几种方法实现归档:
一、对单个对象进行归档
- 归档
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"apple",@"one",@"samsung",@"two",@"xiaomi",@"three",@"HTC",@"four", nil];
//1.获取文件路径
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//2.添加储存的文件名
NSString *path = [docPath stringByAppendingPathComponent:@"data.archiver"];
//归档字典
BOOL flag = [NSKeyedArchiver archiveRootObject:dict toFile:path];
- 解档
//传入路径进行解档
[NSKeyedUnarchiver unarchiveObjectWithFile:path];
这种方法只能对单个对象归档,不能归档多个对象
二、对多个对象的归档
- 归档
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"apple",@"one",@"samsung",@"two",@"xiaomi",@"three",@"HTC",@"four", nil];
NSArray *array = [NSArray arrayWithObjects:@"one",@"two",@"three", nil];
//1.获取文件路径
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//2.添加储存的文件名
NSString *path = [docPath stringByAppendingPathComponent:@"data.archiver"];
//初始化
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:dict forKey:@"dict"];
[archiver encodeObject:array forKey:@"array"];
//完成归档
[archiver finishEncoding];
[data writeToFile:path atomically:YES];
- 解档
//传入路径
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
//开始解档
[unarchiver decodeObjectForKey:@"array"];
[unarchiver decodeObjectForKey:@"dict"];
//完成解档
[unarchiver finishDecoding];
这种方法能归档基本数据类型,但不能对自定义类生成的对象进行归档
三、对自定义对象进行归档
- 创建Person类,定义属性,并且遵循NSCoding协议
@interface Person : NSObject<NSCoding>
//姓名
@property (nonatomic,copy) NSString *name;
//性别
@property (nonatomic,copy) NSString *sex;
//年龄
@property (nonatomic,assign) int age;
@end
- 实现NSCoding协议方法
@implementation Person
//对person对象进行归档时,此方法执行
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.sex forKey:@"sex"];
[aCoder encodeInt:self.age forKey:@"age"];
}
//对person对象进行反归档时,该方法执行
- (instancetype)initWithCoder:(NSCoder *)coder{
self = [super init];
if (self) {
self.name = [coder decodeObjectForKey:@"name"];
self.sex = [coder decodeObjectForKey:@"sex"];
self.age = [coder decodeIntForKey:@"age"];
}
return self;
}
- (NSString *)description{
return [NSString stringWithFormat:@"%@,%@,%d",self.name,self.sex,self.age];
}
@end
- 复杂对象写入文件,实现归档、解档
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
Person *person = [[Person alloc] init];
person.name = @"xiaomeng";
person.sex = @"男";
person.age = 17;
//路径
NSString *path = [NSHomeDirectory() stringByAppendingString:@"person.plist"];
//归档
[NSKeyedArchiver archiveRootObject:person toFile:path];
//解档
person = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"%@",person);//xiaomeng,男,17
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
源码地址