ios-学习篇-归档

 

归档只要将某个类实现nscoding协议就可以了

eg:

//NSCoding协议方法
-(id)initWithCoder:(NSCoder *)aDecoder{

    if (self=[super init]) {
       self.name= [aDecoder decodeObjectForKey:@"name"];
        self.sex=[aDecoder decodeObjectForKey:@"sex"];
        self.age=[aDecoder decodeIntegerForKey:@"age"];
    }
    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder{
    NSLog(@"调用了initWithCoder:方法");
    [aCoder encodeObject:self.name forKey:@"name"];
     [aCoder encodeObject:self.sex forKey:@"sex"];
    [aCoder encodeInteger:self.age forKey:@"age"];

}
NSArray *docaa=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//归档
[NSKeyedArchiver archiveRootObject:p toFile:[docaa[0] stringByAppendingPathComponent:@"datad.d"]];
//解档
person *pp=  (person *) [NSKeyedUnarchiver unarchiveObjectWithFile:[docaa[0] stringByAppendingPathComponent:@"datad.d"]];

序列化的方法,plist,userdefault,coredata,归档

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。