简单实现MJExtension(一) -- 字典转模型

一、简单字典 to 模型

先创建个字典(字典均来自MJExtensionExample)
在 ViewController.m

#pragma mark - 简单字典 转 模型
- (void)simpleKeyValuesToObject {

    NSDictionary *dict = @{
                           @"name" : @"Jack",
                           @"icon" : @"lufy.png",
                           @"age" : @"20",
                           @"height" : @1.55,
                           @"money" : @"100.9",
                           };
}

创建一个NSObject 的分类NSObject+Extension
提供一个类方法 objectFromKeyValue:

实现:

+ (instancetype)objectFromKeyValue:(NSDictionary*)keyValue {

    id obj = [[self alloc]init];

    unsigned int count;

    // 获取成员变量数组
    Ivar *ivarList = class_copyIvarList(self, &count);

    // 遍历
    for (int i = 0; i < count; i++) {
        Ivar ivar = ivarList[i];
        //获取成员变量名字
        NSString *ivarName = [NSString stringWithUTF8String:ivar_getName(ivar)];
        NSLog(@"ivarName = %@",ivarName);//_age _name _icon _height _money

        // 截取 - 获得模型的属性
        NSString *key = [ivarName substringFromIndex:1];
        NSLog(@"key = %@",key);//age name icon height money

        // 获取字典里的值
        id value = keyValue[key];

        // 赋值
        [obj setValue:value forKey:key];
    }

    return obj;
}
简单实现MJExtension(一) -- 字典转模型_第1张图片
YLUserModel 属性

回到ViewController.m

YLUserModel *user = [YLUserModel objectFromKeyValue:dict];
NSLog(@"name = %@,icon = %@,age = %@,height = %@,money = %@,",user.name,user.icon,user.age,user.height,user.money);
简单字典转模型的输出

注意:class_copyIvarList 只能获取当前类是成员变量,不会获取父类

二、复杂字典 to 模型(模型里面包含了模型)

#pragma mark - 复杂字典 转 模型
- (void)complexKeyValuesToObject {

    // 1.定义一个字典
    NSDictionary *dict = @{
                           @"text" : @"是啊,今天天气确实不错!",

                           @"user" : @{
                                   @"name" : @"Jack",
                                   @"age" : @"20"
                                   },
                           };
}

objectFromKeyValue:方法添加代码

+ (instancetype)objectFromKeyValue:(NSDictionary*)keyValue {

    id obj = [[self alloc]init];

    unsigned int count;

    // 获取成员变量数组:注意:只会获取当前类的属性,不会获取父类
    Ivar *ivarList = class_copyIvarList(self, &count);

    // 遍历
    for (int i = 0; i < count; i++) {
        Ivar ivar = ivarList[i];
        // 获取成员变量名字
        NSString *ivarName = [NSString stringWithUTF8String:ivar_getName(ivar)];
        //NSLog(@"ivarName = %@",ivarName);//_age _name _icon _height _money

        // 截取 - 获得模型的属性名
        NSString *key = [ivarName substringFromIndex:1];
        //NSLog(@"key = %@",key);//age name icon height money

        // 获取字典里的值
        id value = keyValue[key];

        // 复杂字典,里面还有字典
        // 所以要获取成员变量的类型
        NSString *ivarType = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];
        // @\"YLUserModel\" 需要截取 ,否则下面的 secIvarClass 是不存在的(这里要好好看下MJExtension,这个解决方式比较low)
        ivarType = [ivarType stringByReplacingOccurrencesOfString:@"@" withString:@""];
        ivarType = [ivarType stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        NSLog(@"ivarType = %@",ivarType);
        if ([value isKindOfClass:[NSDictionary class]] && ![ivarType hasPrefix:@"NS"]) {

            Class secIvarClass = NSClassFromString(ivarType);
            value = [secIvarClass objectFromKeyValue:value];
        }

        // 赋值
        [obj setValue:value forKey:key];
    }

    return obj;
}
简单实现MJExtension(一) -- 字典转模型_第2张图片
YLStatusModel属性.png

回到ViewController.m

YLStatusModel *status = [YLStatusModel objectFromKeyValue:dict];
NSLog(@"text = %@,name = %@",status.text,status.user.name);
复杂字典转模型的输出.png

好了,下班,数组字典转模型 和 替换数组里的key,以后再研究

你可能感兴趣的:(简单实现MJExtension(一) -- 字典转模型)