美文网首页
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 归档案例

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