归档,解档

1. 最基本的归档

   NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject]stringByAppendingPathComponent:@"test.txt"];

    

    // 这个地方可以是任意的类型

    NSArray *arr = [NSArrayarrayWithObjects:@"hhahahah",@"hhahahah",@"hhahahah", nil];

    

    [NSKeyedArchiver archiveRootObject:arrtoFile:path];

    

    NSArray *hh = [NSKeyedUnarchiverunarchiveObjectWithFile:path];


2. 模型归档解档

//解档

-(instancetype)initWithCoder:(NSCoder *)aDecoder

{

    if (self = [superinit])

    {

        self.access_token = [aDecoderdecodeObjectForKey:@"access_token"];

        self.gender = [aDecoderdecodeObjectForKey:@"gender"];

        self.location = [aDecoderdecodeObjectForKey:@"location"];

        self.openid = [aDecoderdecodeObjectForKey:@"openid"];

        self.uid = [aDecoderdecodeObjectForKey:@"uid"];

        self.screen_name = [aDecoderdecodeObjectForKey:@"screen_name"];

        self.profile_image_url = [aDecoderdecodeObjectForKey:@"profile_image_url"];

        self.Phone = [aDecoderdecodeObjectForKey:@"Phone"];

        self.Email = [aDecoderdecodeObjectForKey:@"Email"];

    }

    return self;

}


//归档

-(void)encodeWithCoder:(NSCoder *)aCoder

{

    [aCoder encodeObject:self.access_tokenforKey:@"access_token"];

    [aCoder encodeObject:self.genderforKey:@"gender"];

    [aCoder encodeObject:self.locationforKey:@"location"];

    [aCoder encodeObject:self.openidforKey:@"openid"];

    [aCoder encodeObject:self.uidforKey:@"uid"];

    [aCoder encodeObject:self.screen_nameforKey:@"screen_name"];

    [aCoder encodeObject:self.profile_image_urlforKey:@"profile_image_url"];


    

    [aCoder encodeObject:self.PhoneforKey:@"Phone"];

    [aCoder encodeObject:self.EmailforKey:@"Email"];

}


-(void)setValue:(id)value forUndefinedKey:(NSString *)key

{

    NSLog(@"%s, %@", __func__, key);

}


    Model *model = [[Modelalloc]init];

    model.uid = 12;

    model.ID = 22.2;

    model.createTime = @"dddddd";

    model.nickname = @"rdfvdfvc";

    model.descriptionText =@"hgfdsr";

    model.iconUrl = @"hgfdvxz";


    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject]stringByAppendingPathComponent:@"test.txt"];


    [NSKeyedArchiver archiveRootObject:modeltoFile:path];

    

    Model *model111 = [NSKeyedUnarchiver unarchiveObjectWithFile:path];


3. 模型继承模型的归档

本类集成上面的Model模型 进行归档


@interface hhModel : Model


@property (nonatomic,copy) NSString *createModel;// 提问的时间


@end


-(void)encodeWithCoder:(NSCoder *)aCoder {

    

    [super encodeWithCoder:aCoder];

    [aCoder encodeObject:self.createModelforKey:@"createModel"];

}


-(instancetype)initWithCoder:(NSCoder *)aDecoder {

    

    self = [superinitWithCoder:aDecoder];

    if (self) {

 

        self.createModel = [aDecoderdecodeObjectForKey:@"createModel"];

    }

    return self;

}


    hhModel *model = [[hhModelalloc] init];

    model.uid = 12;

    model.ID = 22.2;

    model.createTime = @"dddddd";

    model.nickname = @"rdfvdfvc";

    model.descriptionText =@"hgfdsr";

    model.iconUrl = @"hgfdvxz";

    model.createModel =@"ffffffffffff";

    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"test.txt"];


    [NSKeyedArchiverarchiveRootObject:model toFile:path];

    

    hhModel *model111 = [NSKeyedUnarchiverunarchiveObjectWithFile:path];



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值