美文网首页
IOS 归档案例

IOS 归档案例

作者: 梦之志 | 来源:发表于2019-04-23 11:16 被阅读0次
        // 归档数据
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentFilePath = paths.firstObject;
        NSString *filePath = [documentFilePath stringByAppendingPathComponent:@"userInfo"];
        
        // 将数据存入模型
        FZUserInfoModel * userInfo = [FZUserInfoModel new];
        
        userInfo.userId = info[@"data"][@"id"];
        
        userInfo.uid = info[@"data"][@"uid"];
        
        userInfo.name = info[@"data"][@"name"];
        
        userInfo.phone_number = info[@"data"][@"phone_number"];
        
        userInfo.address = info[@"data"][@"address"];
        
        userInfo.balance = info[@"data"][@"balance"];
        
        userInfo.create_time = info[@"data"][@"create_time"];
        
        [NSKeyedArchiver archiveRootObject:userInfo toFile:filePath];
// 获取用户数据
+(instancetype)getInfo{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentFilePath = paths.firstObject  ;
    NSString *filePath = [documentFilePath stringByAppendingPathComponent:@"userInfo"];
    FZUserInfoModel * infoModel = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    return infoModel;
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super init];
    if (self) {
        _userId = [aDecoder decodeObjectForKey:@"userId"];
        _uid = [aDecoder decodeObjectForKey:@"uid"];
        _user_type = [aDecoder decodeObjectForKey:@"user_type"];
        _name = [aDecoder decodeObjectForKey:@"name"];
        _phone_number = [aDecoder decodeObjectForKey:@"phone_number"];
        _address = [aDecoder decodeObjectForKey:@"address"];
        _balance = [aDecoder decodeObjectForKey:@"balance"];
        _create_time = [aDecoder decodeObjectForKey:@"create_time"];
    }
    return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.userId forKey:@"userId"];
    [aCoder encodeObject:self.uid forKey:@"uid"];
    [aCoder encodeObject:self.user_type forKey:@"user_type"];
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.phone_number forKey:@"phone_number"];
    [aCoder encodeObject:self.address forKey:@"address"];
    [aCoder encodeObject:self.balance forKey:@"balance"];
    [aCoder encodeObject:self.create_time forKey:@"create_time"];
}

相关文章

  • IOS 归档案例

  • 2021-08-20

    iOS swift NSKeyedArchiver 使用 iOS 中swift的归档与解挡 归档: 将对象按照一定...

  • ios中Swift的归档与解档

    ios中Swift的归档与解档 归档 解档 init()方法 设置属性

  • iOS NSKeyedArchiver数据归档

    iOS中利用NSKeyedArchiver和NSKeyedUnarchiver进行数据的归档和解档操作。 归档 所...

  • iOS归档总结

    iOS归档总结,高效开发,欢迎star

  • iOS对象归档

    终结一下iOS的对象归档问题. 1:这个对象为什么需要归档 2:使用归档的时候需要注意什么 3:归档以后写进本地,...

  • iOS 归档和解归档

    归档与解档是IOS中一种序列化与反序列化的方式。对象要实现序列化需要遵循NSCoding协议,而绝大多数Found...

  • 归档-解归档

    归档-解归档 在iOS开发中,数据持久化方法很多,比如:SQL、KeyChina、CoreData、NSUserD...

  • 浅析iOS中的归档与反归档

    今天我们要搞一搞iOS开发当中的归档和反归档,在进入正题之前,我们需要了解一下归档和反归档是干什么用的。 所谓归档...

  • Percona-toolkit工具详解

    1、pt-archiver(归档表) 使用案例:1.归档到数据库例句1:pt-archiver --source ...

网友评论

      本文标题:IOS 归档案例

      本文链接:https://www.haomeiwen.com/subject/pyamgqtx.html