Board logo

标题: 用runtime遍历用户属性的方法与传统方法的比较 (3) [打印本页]

作者: look_w    时间: 2019-2-19 17:32     标题: 用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




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0