IOS 归档案例

2019-04-23  本文已影响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"];
}
上一篇 下一篇

猜你喜欢

热点阅读