zoukankan      html  css  js  c++  java
  • 归档

    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];
     
  • 相关阅读:
    以此来励志吧!!!(选自:知乎)
    【P1303】苹果二叉树
    【P1813】8的倍数
    2016.9.4 の 測試
    后缀数组
    个中模板
    基数排序
    【NOIP2014D2T3】解方程
    【HAOI2006】【BZOJ1051】【p1233】最受欢迎的牛
    java安全性-引用-分层-解耦
  • 原文地址:https://www.cnblogs.com/huoxingdeguoguo/p/4721579.html
Copyright © 2011-2022 走看看