Runtime进阶三:字典转模型

本文深入探讨了如何使用Runtime在iOS开发中将请求到的数据转换为模型。介绍了两种方法:一是利用KVC,但当属性不匹配时会报错,可以通过重写特定方法来避免;二是通过Runtime遍历模型属性,动态地为模型赋值,从而实现字典到模型的转换。
摘要由CSDN通过智能技术生成

Runtime进阶三:字典转模型

写在前面,这是一篇介绍如何通过Runtime来实现,从请求到数据,到生成模型,再到给模型赋值的全过程。如果只在意结果,可以分别用三方软件生成模型,再用MJExtension等三方库给模型赋值。

首先第一步:请求到数据后生成模型

// 拼接属性字符串代码
    NSMutableString *strM = [NSMutableString string];
    // 1.遍历字典,把字典中的所有key取出来,生成对应的属性代码
    [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
    NSString *type;
        if ([obj isKindOfClass:NSClassFromString(@"__NSCFString")]) {
            type = @"NSString";
        }else if ([obj isKindOfClass:NSClassFromString(@"__NSCFArray")]){
            type = @"NSArray";
        }else if ([obj isKindOfClass:NSClassFromString(@"__NSCFNumber")]){
            type = @"int";
        }else if ([obj isKindOfClass:NSClassFromString(@"__NSCFDictionary")]){
            type = @"NSDictionary";
        }else if ([obj isKindOfClass:NSClassFromString(@"__NSCFBoolean")]){
            type = @"BooL";
        }
        // 属性字符串
        NSString *str;
        if ([type containsString:@"NS"]) {
            if ([type isEqualToString:@"NSString"]) {
                str = [NSString stringWithFormat:@"@property (nonatomic, copy) %@ *%@;",type,key];
            }else {
                str = [NSString stringWithFormat:@"@property (nonatomic, strong) %@ *%@;",type,key];
            }
        }else{
            str = [NSString stringWithFormat:@"@property (nonatomic, assign) %@ %@;",type,key];
        }
        // 每生成属性字符串,就自动换行。
        [strM appendFormat:@"\n%@\n",str];  
    }];
    // 把拼接好的字符串打印出来,就好了。
    NSLog(@"%@",strM);

第二步,实现字典转模型

这一步有两种方式,方式一:通过KVC实现
但是这种方式有个弊端,当模型中属性和字典中不一致时,就会报错。
解决途径:重写如下方法,当不一致时,系统会自动调用
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
}
方式二:通过Runtime实现字典转模型

实现思路:遍历模型中所有属性,根据模型中所有的属性名去字典中查key,给模型的属性赋值

首先第一步,遍历模型中所有的属性

//class_copyIvarList(self, &count)该方法第一个参数是要获取哪个类中的成员属性,第二个参数是这个类中有多少成员属性,需要传入地址,返回值Ivar是个数组,会将所有成员属性放入这个数组中
id objc = [[self alloc] init];

    unsigned int count;

    // 获取类中的所有成员属性
    Ivar *ivarList = class_copyIvarList(self, &count);

    for (int i = 0; i < count; i++) {
        Ivar ivar = ivarList[i];

        // 获取成员属性名
        NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];

        // 处理成员属性名->字典中的key
        // 从第一个角标开始截取
        NSString *key = [name substringFromIndex:1];

        // 根据成员属性名去字典中查找对应的value
        id value = dict[key];
       // 判断字典中是否存在字典,如果存在,转为模型字典属性生成的是@"@\"xxxx\""类型,需要裁减为@"xxxx"
if ([value isKindOfClass:[NSDictionary class]]) {
            // 获取成员属性类型
            NSString *type = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];
            // 生成的是这种@"@\"User\"" 类型 -》 @"User"  在OC字符串中 \" -> ",\是转义的意思,不占用字符
            // 裁剪类型字符串
            NSRange range = [type rangeOfString:@"\""];

            type = [type substringFromIndex:range.location + range.length];
            range = [type rangeOfString:@"\""];
            // 裁剪到哪个角标,不包括当前角标
            type = [type substringToIndex:range.location];
            // 根据字符串类名生成类对象
            Class modelClass = NSClassFromString(type); 
            if (modelClass) {
                value  =  [modelClass modelWithDict:value];
            }
        }
        //通过给分类添加一个协议,来实现将数组中的字典转为模型
if ([value isKindOfClass:[NSArray class]])
        {
            // 判断对应类有没有实现字典数组转模型数组的协议
            if ([self respondsToSelector:@selector(arrayContainModelClass)]) {

                // 转换成id类型,就能调用任何对象的方法
                id idSelf = self;
                // 获取数组中字典对应的模型
                NSString *type =  [idSelf arrayContainModelClass][key];
                // 生成模型
                Class classModel = NSClassFromString(type);
                NSMutableArray *arrM = [NSMutableArray array];
                // 遍历字典数组,生成模型数组
                for (NSDictionary *dict in value) {
                    // 字典转模型
                    id model =  [classModel modelWithDict:dict];
                    [arrM addObject:model];
                }
                // 把模型数组赋值给value
                value = arrM;
            }
        }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值