所谓归档:将复杂对象转化为NSData类型数据(复杂-->归档-->NSData--->WriteToFile)
注意:归档是将对象转化为数据字节,以文件的形式存储在磁盘上,-
所谓反归档:将NSData类型数据转化为复杂对象(读取文件-->NSData-->反归档--->复杂对象)
再说归档之前,先说下iOS的数据持久化方式:
那么问题来了,什么是数据持久化?
数据持久化(侠义的理解):仅仅指把域对象永久保存到数据库中,(广义的理解):包括和数据库相关的各种操作
优点:
1:数据持久化可以减少访问数据库数据的次数,增加应用程序执行的速度;
2:代码重用性高,能够完成大部分数据库操作;
3:低耦合,使持久化不依赖于底层数据库和上层业务逻辑实现,更换数据库时只需要修改配置文件而不用修改代码
那么iOS的数据化持久方式有哪些呢?
1:属性列表(Plist文件),NSUserDefault
2:归档(NSKeyedArchiver)
3:数据库(SQlite,CoreData,FMDB(第三方))
今天重点说下归档和结档的问题
归档的三种方式:
1:对foundation框架的对象进行归档
2:对自定义的对象进行归档
3:对自定义内容进行归档
** 注意:归档生成的文件是加密的**
下边举简单的例子分别说下归档的三种方式
#pragma mark---对自定义对象进行归档反归档----
- (void)archive{
//先创建一个Person类
/*
第一:复杂对象所属的类一定要遵循NSCoding协议
第二:实现协议中的方法
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (instancetype)initWithCoder:(NSCoder *)aDecoder;
//归档(由叫序列化)的步骤
//对Person类对象进行归档时执行此方法.并对Person类中想要进行归档的所有属性,进行序列化操作
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeObject:_gender forKey:@"gender"];
[aCoder encodeObject:_age forKey:@"age"];
}
//反归档(由叫反序列化)的步骤
//对Person类对象进行反归档时执行此方法.并对Person类中想要进行反归档的所有属性,进行反序列化操作
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]) {
_name = [aDecoder decodeObjectForKey:@"name"];
_gender = [aDecoder decodeObjectForKey:@"gender"];
_age = [aDecoder decodeObjectForKey:@"age"];
}
return self;
}
*/
person *per = [person new];
per.name = @"甲甲";
per.age = @"20";
per.gender = @"女";
//归档
//1:准备路径
NSString *path = NSHomeDirectory();
path = [path stringByAppendingString:@"Singel.plist"];
//2:准备存储数据对象(用可变数组进行接收)
NSMutableData *data = [NSMutableData new];
//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];
aper = [unarchiver decodeObjectForKey:@"person"];
//4:结束归档
[unarchiver finishDecoding];
NSLog(@"%@",aper.name);
}
#pragma mark----对foundation框架中的对象进行归档和反归档
- (void)fundationClass{
//归档
//1:创建路径
NSString *Doucuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
NSString *path = [Doucuments stringByAppendingString:@"file.plist"];
//2:归档(不需要结束方法)
NSArray *array = @[@"jiajia",@"jiejie",@"pangpang"];
BOOL result = [NSKeyedArchiver archiveRootObject:array toFile:path];
if (result) {
NSLog(@"框架中的归档%@",path);
}
//反归档
NSArray *newArray = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"框架的结档%@",newArray);
}
#pragma mark----对自定义的内容进行归档和反归档
- (void)customContent{
//归档
//1:获取路径
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
path = [path stringByAppendingString:@"custom.plist"];
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
[archiver encodeObject:@"111" forKey:@"name"];
[archiver encodeObject:@"2" forKey:@"gender"];
[archiver encodeObject:@"100" forKey:@"age"];
[archiver finishEncoding];
BOOL result = [data writeToFile:path atomically:YES];
if (result) {
NSLog(@"归档%@",path);
}
NSData *newdata = [NSData dataWithContentsOfFile:path];
NSKeyedUnarchiver *unkeyed = [[NSKeyedUnarchiver alloc]initForReadingWithData:newdata];
// NSString *str1 = [unkeyed decodeObjectForKey:@"name"];
// NSString *str2 = [unkeyed decodeObjectForKey:@"gender"];
// NSString *str3 = [unkeyed decodeObjectForKey:@"age"];
person *pp = [person new];
pp.name = [unkeyed decodeObjectForKey:@"name"];
pp.gender = [unkeyed decodeObjectForKey:@"gender"];
pp.age = [unkeyed decodeObjectForKey:@"age"];
[unkeyed finishDecoding];
NSLog(@"name:%@ gender:%@ age:%@",pp.name,pp.gender,pp.age);
// NSLog(@"name:%@ gender:%@ age:%@",str1,str2,str3);
}