首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

用runtime遍历用户属性的方法与传统方法的比较 (3)

用runtime遍历用户属性的方法与传统方法的比较 (3)

原始方法一个一个去写

    // 归档(NSCoding 协议方法):
    - (void)encodeWithCoderNSCoder *)aCoder {
        [aCoder encodeObject:self.user_id forKey"user_id"];
        [aCoder encodeObject:self.nickname forKey"nickname"];
        [aCoder encodeObject:self.moblie forKey"moblie"];
        [aCoder encodeObject:self.lng forKey"lng"];
        [aCoder encodeObject:self.lat forKey"lat"];
        [aCoder encodeObject:self.token forKey"token"];
        [aCoder encodeObject:self.avatar forKey"avatar"];
        [aCoder encodeObject:self.describe forKey"describe"];
        [aCoder encodeObject:self.signature forKey"signature"];
        [aCoder encodeObject:self.gender forKey"gender"];
        [aCoder encodeObject:self.school forKey:@"school"];
        [aCoder encodeObject:self.department forKey:@"department"];
        [aCoder encodeObject:self.start_time forKey:@"start_time"];
        [aCoder encodeObject:self.education_id forKey:@"education_id"];
    }
     
     
    // 反归档(NSCoding 协议方法):
    - (nullable instancetype)initWithCoderNSCoder *)aDecoder {
        if (self = [super init]) {
            self.user_id = [aDecoder decodeObjectForKey:@"user_id"];
            self.nickname = [aDecoder decodeObjectForKey:@"nickname"];
            self.moblie = [aDecoder decodeObjectForKey:@"moblie"];
            self.lng = [aDecoder decodeObjectForKey:@"lng"];
            self.lat = [aDecoder decodeObjectForKey:@"lat"];
            self.token = [aDecoder decodeObjectForKey:@"token"];
            self.avatar = [aDecoder decodeObjectForKey:@"avatar"];
            self.describe = [aDecoder decodeObjectForKey:@"describe"];
            self.signature = [aDecoder decodeObjectForKey:@"signature"];
            self.gender = [aDecoder decodeObjectForKey:@"gender"];
            self.school = [aDecoder decodeObjectForKey:@"school"];
            self.department = [aDecoder decodeObjectForKey:@"department"];
            self.start_time = [aDecoder decodeObjectForKey:@"start_time"];
            self.education_id = [aDecoder decodeObjectForKey:@"education_id"];
     
        }
        return self;
    }
     

重写description方法打印

    - (NSString *)description {
        return [NSString stringWithFormat:@"%@, %@, %@, %@, %@, %@, %@", _user_id, _nickname, _avatar, _moblie, _token, _lat, _lng];
    }
     
    @end
返回列表