标签:
1 #import <Foundation/Foundation.h> 2 3 @interface Person : NSObject<NSCoding> 4 5 /** 6 * 姓名 7 */ 8 @property (nonatomic, strong) NSString *name ; 9 10 /** 11 * 地址 12 */ 13 @property (nonatomic, strong) NSString *address ; 14 15 /** 16 * 年龄 17 */ 18 @property (nonatomic, assign) int age; 19 20 21 -(instancetype)initWithName:(NSString *)name address:(NSString *)address age:(int)age; 22 23 @end
Person.m
1 #import "Person.h" 2 3 @implementation Person 4 5 -(instancetype)initWithName:(NSString *)name address:(NSString *)address age:(int)age{ 6 if (self = [super init]) { 7 self.name = name; 8 self.address = address; 9 self.age = age; 10 } 11 return self; 12 } 13 14 //告知编译器,我们需要归档当前对象的哪些属性 15 -(void)encodeWithCoder:(NSCoder *)aCoder{ 16 17 [aCoder encodeObject:self.name forKey:@"name"]; 18 [aCoder encodeObject:self.address forKey:@"address"]; 19 [aCoder encodeInt: self.age forKey:@"age"]; 20 } 21 22 //告知编译器,解档时对应的属性 23 -(instancetype)initWithCoder:(NSCoder *)aDecoder{ 24 25 self.name = [aDecoder decodeObjectForKey:@"name"]; 26 self.address = [aDecoder decodeObjectForKey:@"address"]; 27 self.age = [aDecoder decodeIntForKey:@"age"]; 28 return self; 29 } 30 31 @end
控制器代码1:归档
1 -(void)personArchive{ 2 //创建及初始化对象 3 Person *p = [[Person alloc] initWithName: @"jack" address:@"Mars" age:20]; 4 5 //定义归档路径 6 NSString *fullPath = [self fullPathWithFileName:@"person.data"]; 7 8 //进行归档 9 [NSKeyedArchiver archiveRootObject:p toFile:fullPath]; 10 11 NSLog(@"对象归档成功"); 12 }
控制器代码2:解档
1 -(void)personUnarchive{ 2 3 NSString *fullPath = [self fullPathWithFileName:@"person.data"]; 4 5 Person *p = [NSKeyedUnarchiver unarchiveObjectWithFile:fullPath]; 6 7 NSLog(@"name = %@,address = %@,age = %d",p.name ,p.address ,p.age); 8 9 }
注: fullPathWithFileName: 方法为便捷获取路径方法,代码如下
1 -(NSString *)fullPathWithFileName:(NSString *)fileName{ 2 3 NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]; 4 5 return [documentDir stringByAppendingPathComponent:fileName]; 6 7 }
依次调用我们的归档和解档方法
[self personArchive];//归档 [self personUnarchive];//解档
结果如下:
至此我们已经实现了简单的单个对象的归档和解档操作,下面我们来看下继承关系下的对象的归档解档操作
定义一个Student类继承自Person类,代码如下
Student.h
1 #import "Person.h" 2 3 @interface Student : Person 4 5 /** 6 * 学校名称 7 */ 8 @property (nonatomic, strong) NSString *schoolName; 9 10 -(instancetype)initWithName:(NSString *)name address:(NSString *)address age:(int)age schoolName:(NSString *)schoolName; 11 12 @end
Student.m
1 #import "Student.h" 2 3 @implementation Student 4 5 -(instancetype)initWithName:(NSString *)name address:(NSString *)address age:(int)age schoolName:(NSString *)schoolName{ 6 if (self = [super initWithName:name address:address age:age]) { 7 self.schoolName =schoolName; 8 } 9 return self; 10 } 11 12 13 -(void)encodeWithCoder:(NSCoder *)aCoder{ 14 15 [super encodeWithCoder:aCoder]; 16 17 [aCoder encodeObject:self.schoolName forKey:@"schoolName"]; 18 } 19 20 -(instancetype)initWithCoder:(NSCoder *)aDecoder 21 { 22 self = [super initWithCoder:aDecoder]; 23 self.schoolName = [aDecoder decodeObjectForKey:@"schoolName"]; 24 return self; 25 } 26 @end
控制器方法1:归档
1 -(void)studentArchive{ 2 3 Student *stu = [[Student alloc] initWithName:@"小明" address:@"走廊" age:10 schoolName:@"剑桥大学"]; 4 5 NSString *fullpath = [self fullPathWithFileName:@"stu.data"]; 6 7 [NSKeyedArchiver archiveRootObject:stu toFile:fullpath]; 8 9 NSLog(@"归档成功"); 10 }
控制器方法2:解档
1 -(void)studentUnarchive{ 2 3 NSString *fullpath = [self fullPathWithFileName:@"stu.data"]; 4 Student *stu = [NSKeyedUnarchiver unarchiveObjectWithFile:fullpath]; 5 NSLog(@"name = %@,age = %d,address = %@,schoolName = %@",stu.name,stu.age ,stu.address,stu.schoolName); 6 }
依次调用上述两个方法,运行结果如下
演示代码如下:
1.归档
1 -(void)multipleArchive{ 2 3 Student *s0 = [[Student alloc]initWithName:@"name0" address:@"address0" age:10 schoolName:@"schoolName0"]; 4 Student *s1 = [[Student alloc]initWithName:@"name1" address:@"address1" age:20 schoolName:@"schoolName1"]; 5 6 7 NSMutableData *data = [NSMutableData data]; 8 NSKeyedArchiver *archive = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; 9 10 [archive encodeObject:s0 forKey:@"student0"]; 11 [archive encodeObject:s1 forKey:@"student1"]; 12 13 [archive finishEncoding]; 14 15 NSString *fullpath = [self fullPathWithFileName:@"multiple.data"]; 16 17 [data writeToFile:fullpath atomically:YES]; 18 19 NSLog(@"多对象归档成功"); 20 21 }
02.解档:
1 -(void)multipleUnArchive{ 2 3 NSString *fullpath = [self fullPathWithFileName:@"multiple.data"]; 4 NSData *data = [NSData dataWithContentsOfFile:fullpath]; 5 NSKeyedUnarchiver *unArchive = [[NSKeyedUnarchiver alloc]initForReadingWithData:data]; 6 Student *stu0 = [unArchive decodeObjectForKey:@"student0"]; 7 Student *stu1 = [unArchive decodeObjectForKey:@"student1"]; 8 [unArchive finishDecoding]; 9 NSLog(@"name = %@,age = %d,address = %@,schoolName = %@",stu0.name,stu0.age,stu0.address,stu0.schoolName); 10 NSLog(@"name = %@,age = %d,address = %@,schoolName = %@",stu1.name,stu1.age,stu1.address,stu1.schoolName); 11 12 }
运行结果如下图:
标签:
原文地址:http://www.cnblogs.com/cboyce/p/5128709.html