前言:个人觉得开发人员最大的悲哀莫过于懂得使用却不明白其中的原理.在代码之前我觉得还是有必要简单阐述下相关的一些知识点. 因为文章或深或浅总有适合的人群.若有朋友发现了其中不正确的观点还望多多指出,不胜感激.
承接上篇博客我们来看看IOS开发中是如何将一个自定义的对象进行归档的
本篇博客将介绍以下几个方面的内容
本篇博客将介绍以下几个方面的内容
1)普通的单个对象归档操作
2)拥有继承关系的对象归档
3)同时将多个对象进行归档
2)拥有继承关系的对象归档
3)同时将多个对象进行归档
1.普通的单个对象归档操作
首先我们来看下最简单的单个对象归档操作
1.自定义一个跟小明一样有名的类(Person)
1.自定义一个跟小明一样有名的类(Person)
直接上代码 :
Person.h
#import <Foundation/Foundation.h> @interface Person : NSObject<NSCoding> /**
* 姓名
*/
@property (nonatomic, strong) NSString *name ; /**
* 地址
*/
@property (nonatomic, strong) NSString *address ; /**
* 年龄
*/
@property (nonatomic, assign) int age; -(instancetype)initWithName:(NSString *)name address:(NSString *)address age:(int)age;//初始化方法 @end
Person.m
#import "Person.h" @implementation Person -(instancetype)initWithName:(NSString *)name address:(NSString *)address age:(int)age{
if (self = [super init]) {
self.name = name;
self.address = address;
self.age = age;
}
return self;
} //告知编译器,我们需要归档当前对象的哪些属性
-(void)encodeWithCoder:(NSCoder *)aCoder{ [aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.address forKey:@"address"];
[aCoder encodeInt: self.age forKey:@"age"];
} //告知编译器,解档时对应的属性
-(instancetype)initWithCoder:(NSCoder *)aDecoder{ self.name = [aDecoder decodeObjectForKey:@"name"];
self.address = [aDecoder decodeObjectForKey:@"address"];
self.age = [aDecoder decodeIntForKey:@"age"];
return self;
} //重写description,方便调试查看对象属性信息
-(NSString *)description{
return [NSString stringWithFormat:@"name : %@, address : %@,age : %d",self.name,self.address,self.age];
} @end
控制器代码1:归档
-(void)personArchive{
//创建及初始化对象
Person *p = [[Person alloc] initWithName: @"jack" address:@"Mars" age:]; //定义归档路径
NSString *fullPath = [self fullPathWithFileName:@"person.data"]; //进行归档
[NSKeyedArchiver archiveRootObject:p toFile:fullPath]; NSLog(@"对象归档成功");
}
控制器代码2:解档
-(void)personUnarchive{
//指定路径
NSString *fullPath = [self fullPathWithFileName:@"person.data"];
//解档操作
Person *p = [NSKeyedUnarchiver unarchiveObjectWithFile:fullPath];
//打印结果
NSLog(@"%@",p); }
注: fullPathWithFileName: 方法为便捷获取路径方法,代码如下
-(NSString *)fullPathWithFileName:(NSString *)fileName{ NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
//拼接完整路径并返回
return [documentDir stringByAppendingPathComponent:fileName]; }
依次调用我们的归档和解档方法
[self personArchive];//归档
[self personUnarchive];//解档
结果如下:
至此我们已经实现了简单的单个对象的归档和解档操作,下面我们来看下继承关系下的对象的归档解档操作
2.拥有继承关系的对象归档和解档
定义一个Student类继承自Person类,代码如下
Student.h
#import "Person.h" @interface Student : Person /**
* 学校名称
*/
@property (nonatomic, strong) NSString *schoolName;
//快速初始化方法
-(instancetype)initWithName:(NSString *)name address:(NSString *)address age:(int)age schoolName:(NSString *)schoolName; @end
Student.m
#import "Student.h" @implementation Student -(instancetype)initWithName:(NSString *)name address:(NSString *)address age:(int)age schoolName:(NSString *)schoolName{
if (self = [super initWithName:name address:address age:age]) {
self.schoolName =schoolName;
}
return self;
} -(void)encodeWithCoder:(NSCoder *)aCoder{ [super encodeWithCoder:aCoder]; [aCoder encodeObject:self.schoolName forKey:@"schoolName"];
} -(instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
self.schoolName = [aDecoder decodeObjectForKey:@"schoolName"];
return self;
} //重写description,方便调试查看属性信息
-(NSString *)description{ //先拿到父类拥有的属性描述
NSString *str = [super description];
//添加子类特有的描述,返回
return [str stringByAppendingString:[NSString stringWithFormat:@",schoolName : %@",self.schoolName]];
}
@end
控制器方法1:归档
-(void)studentArchive{
//声明一个子类对象
Student *stu = [[Student alloc] initWithName:@"小明" address:@"走廊" age: schoolName:@"剑桥大学"];
//定义路径
NSString *fullpath = [self fullPathWithFileName:@"stu.data"];
//归档操作
[NSKeyedArchiver archiveRootObject:stu toFile:fullpath]; NSLog(@"归档成功");
}
控制器方法2:解档
-(void)studentUnarchive{ NSString *fullpath = [self fullPathWithFileName:@"stu.data"];//指定路径
Student *stu = [NSKeyedUnarchiver unarchiveObjectWithFile:fullpath];//解档
NSLog(@"%@",stu);//打印信息
}
依次调用上述两个方法,运行结果如下
3.同时将多个对象归档
演示代码如下:
1.归档
-(void)multipleArchive{
//创建并初始化两个对象
Student *s0 = [[Student alloc]initWithName:@"name0" address:@"address0" age: schoolName:@"schoolName0"];
Student *s1 = [[Student alloc]initWithName:@"name1" address:@"address1" age: schoolName:@"schoolName1"]; //创建一个NSMutableData对象
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archive = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];//关联 [archive encodeObject:s0 forKey:@"student0"];
[archive encodeObject:s1 forKey:@"student1"]; [archive finishEncoding];
//指定路径
NSString *fullpath = [self fullPathWithFileName:@"multiple.data"];
//到这一步数据已缓存到data中,将其写入文件
[data writeToFile:fullpath atomically:YES]; NSLog(@"多对象归档成功"); }
02.解档:
-(void)multipleUnArchive{ NSString *fullpath = [self fullPathWithFileName:@"multiple.data"];//指定路径
NSData *data = [NSData dataWithContentsOfFile:fullpath];//将数据解析成NSData对象
NSKeyedUnarchiver *unArchive = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];//关联
Student *stu0 = [unArchive decodeObjectForKey:@"student0"];
Student *stu1 = [unArchive decodeObjectForKey:@"student1"];
[unArchive finishDecoding];
NSLog(@"%@",stu0);
NSLog(@"%@",stu1); }
运行结果如下图:
4.分析与简单总结
1.将一个对象归档的前提:
遵守NSCoding协议
实现两个方法:
encodeWithCoder://指定需要归档的属性
initWithCoder://指定需要解档的属性
实现两个方法:
encodeWithCoder://指定需要归档的属性
initWithCoder://指定需要解档的属性
2.继承关系的对象归档注意:
在实现上述两个方法的时候记得调用其父类的方法
eg:
[super encodeWithCoder:aCoder];
self = [super initWithCoder:aDecoder];
eg:
[super encodeWithCoder:aCoder];
self = [super initWithCoder:aDecoder];
3.多对象归档依赖于:NSData对象