Board logo

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

作者: look_w    时间: 2019-2-19 17:31     标题: 用runtime遍历用户属性的方法与传统方法的比较

初始化方法

    #import "User.h"
    #import <objc/message.h>
     
    @implementation User
     
    - (id) initWithDicionaryNSDictionary*)dic {
        self = [super init];
        if (self) {
            //更新用户信息:
            [self updateUserInfo:dic];
        }
        return self;
    }

更新用户信息

    - (void)updateUserInfoNSDictionary *)dic {
        id userId =  [[dic objectForKey"data"] objectForKey"user_id"];
        if ([userId isKindOfClass:[NSNull class]]) self.user_id = @"0000";
        self.user_id = userId;
        
        NSString *nickName = [[dic objectForKey"data"] objectForKey"nickname"];
        if ([nickName isKindOfClass:[NSNull class]]) self.nickname = @"";
        self.nickname = nickName;
        
        id mobile = [[dic objectForKey"data"] objectForKey"mobile"];
        if ([mobile isKindOfClass:[NSNull class]]) self.moblie = @"0000";
        self.moblie = mobile;
        
        id lng = [[dic objectForKey"data"] objectForKey"lng"];
        if ([lng isKindOfClass:[NSNull class]]) self.lng = @"00.00";
        self.lng = lng;
        
        id lat = [[dic objectForKey"data"] objectForKey"lat"];
        if ([lat isKindOfClass:[NSNull class]]) self.lat = @"00.00";
        self.lat = lat;
        
        NSString *avater = [[dic objectForKey:@"data"] objectForKey:@"avatar"];
        if ([avater isKindOfClass:[NSNull class]]) self.avatar = @"";
        self.avatar = avater;
        
        NSString *token = [dic objectForKey:@"token"];
        if ([token isKindOfClass:[NSNull class]]) self.token = @"";
        self.token = token;
        
        NSString *describe = [[dic objectForKey:@"data"] objectForKey:@"describe"];
        if ([describe isKindOfClass:[NSNull class]]) self.describe = @"";
        self.describe = describe;
        
        NSString *signature = [[dic objectForKey:@"data"] objectForKey:@"signature"];
        if ([signature isKindOfClass:[NSNull class]]) self.signature = @"";
        self.signature = signature;
        
        NSString *gender = [[dic objectForKey:@"data"] objectForKey:@"gender"];
        if ([gender isKindOfClass:[NSNull class]])  self.gender = @"";
        self.gender = gender;
        
        NSString *school = [[dic objectForKey:@"data"] objectForKey:@"school"];
        if ([school isKindOfClass:[NSNull class]]) self.school = @"";
        self.school = school;
        
        NSString *department = [[dic objectForKey:@"data"] objectForKey:@"department"];
        if ([department isKindOfClass:[NSNull class]]) self.department = @"";
        self.department = department;
        
        NSString *start_time = [[dic objectForKey:@"data"] objectForKey:@"start_time"];
        if ([start_time isKindOfClass:[NSNull class]]) self.start_time = @"";
        self.start_time = start_time;
        
        NSString *education_id = [[dic objectForKey:@"data"] objectForKey:@"education_id"];
        if ([education_id isKindOfClass:[NSNull class]])  self.education_id = @"";
        self.education_id = education_id;
    }
---------------------




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