码迷,mamicode.com
首页 > 移动开发 > 详细

ios-学习篇-归档

时间:2015-02-08 12:46:04      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

 

归档只要将某个类实现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,归档

ios-学习篇-归档

标签:

原文地址:http://www.cnblogs.com/liyang31tg/p/4279838.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!