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,归档

posted @ 2015-02-08 11:49  离子  阅读(157)  评论(0编辑  收藏  举报