签协议
<
NSCoding
>
现在model 中 实现
model中有两个属性
-(void)encodeWithCoder:(NSCoder*)aCoder
{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age" ];
}
-( id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
}
return self;
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age" ];
}
-( id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
}
return self;
}
//归档
BOOL result = [NSKeyedArchiver archiveRootObject:stu toFile:pathStr];
//反归档
Student *stu1 = [NSKeyedUnarchiver unarchiveObjectWithFile:pathStr];