1、将要归档的对象遵守协议@interface Account : NSObject<NSCoding>

2、在m方法中实现2个方法

//从文件中解析对象的时候调用
- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    
    if (self) {
        self.access_token = [decoder decodeObjectForKey:@"access_token"];
        self.expires_in   = [decoder decodeInt64ForKey:@"expires_in"];//这用int64是因为属性为longlong类型
        self.remind_in = [decoder decodeInt64ForKey:@"remind_in"];
        self.uid = [decoder decodeInt64ForKey:@"uid"];
    }
    
    return self;
}

//将对象写入文件时调用
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.access_token forKey:@"access_token"];
    
    [aCoder encodeInt64:self.expires_in forKey:@"expires_in"];
    
    [aCoder encodeInt64:self.remind_in forKey:@"remind_in"];
    
    [aCoder encodeInt64:self.uid forKey:@"uid"];
    
    
}

第三步:归档

          Account *account = [Account accountWithDict:responseObject];
            
            //储存模型数据
            
            NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            
            NSString *file = [doc stringByAppendingPathComponent:@"account.data"];
            
            [NSKeyedArchiver archiveRootObject:account toFile:file];

 4、解归档

 [NSKeyedUnarchiver unarchiveObjectWithFile:file];
 
posted on 2015-08-11 17:10  火星的蝈蝈  阅读(141)  评论(0编辑  收藏  举报