iOS中字典转模型的方法

作者: 打电话记错号码的人 | 来源:发表于2016-07-25 17:23 被阅读1270次

作品链接:http://www.jianshu.com/users/1e0f5e6f73f6/top_articles

1 自动打印属性字符串分类

  • 提供一个分类,专门根据字典生成对应的属性字符串。
@implementation NSObject (Property)

+ (void)PH_createPropertyCodeWithDict:(NSDictionary *)dict
{
    NSMutableString *strM = [NSMutableString string];
    // 遍历字典
    [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull propertyName, id  _Nonnull value, BOOL * _Nonnull stop) {
        NSLog(@"%@,%@",propertyName,[value class]);

        NSString *code;
        if ([value isKindOfClass:NSClassFromString(@"__NSCFString")]) {
            code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSString *%@;",propertyName]
            ;
        } else if ([value isKindOfClass:NSClassFromString(@"__NSCFNumber")]){
            code = [NSString stringWithFormat:@"@property (nonatomic, assign) int %@;",propertyName]
            ;
        }else if ([value isKindOfClass:NSClassFromString(@"__NSCFArray")]){
            code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSArray *%@;",propertyName]
            ;
        }else if ([value isKindOfClass:NSClassFromString(@"__NSCFDictionary")]){
            code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSDictionary *%@;",propertyName]
            ;
        } else if ([value isKindOfClass:NSClassFromString(@"__NSCFBoolean")]){
            code = [NSString stringWithFormat:@"@property (nonatomic, assign) BOOL %@;",propertyName]
            ;
        }
        [strM appendFormat:@"\n%@\n",code];
    }];

    NSLog(@"%@",strM);
}
@end

2 字典转模型的方式一:KVC

@implementation Status


+ (instancetype)statusWithDict:(NSDictionary *)dict
{
    Status *status = [[self alloc] init];

    [status setValuesForKeysWithDictionary:dict];

    return status;

}

@end

  • KVC字典转模型弊端:必须保证,模型中的属性和字典中的key一一对应。
    • 如果不一致,就会调用[<Status 0x7fa74b545d60> setValue:forUndefinedKey:]
      key找不到的错。
    • 分析:模型中的属性和字典的key不一一对应,系统就会调用setValue:forUndefinedKey:报错。
    • 解决:重写对象的setValue:forUndefinedKey:,把系统的方法覆盖,
      就能继续使用KVC,字典转模型了。
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{

}

3 字典转模型的方式二:Runtime

  • KVC:遍历字典中所有key,去模型中查找有没有对应的属性名
  • runtime:遍历模型中所有属性名,去字典中查找

1.思路与步骤

<!--* 思路:利用运行时,遍历模型中所有属性,根据模型的属性名,去字典中查找key,取出对应的值,给模型的属性赋值。-->
<!--* 步骤:提供一个NSObject分类,专门字典转模型,以后所有模型都可以通过这个分类转。-->

2.分类

+ (instancetype)PH_modelWithDict:(NSDictionary *)dict
{
    // 创建对应类的对象
    id objc = [[self alloc] init];

    // runtime:遍历模型中所有成员属性,去字典中查找
    // 属性定义在哪,定义在类,类里面有个属性列表(数组)

    // 遍历模型所有成员属性
    // ivar:成员属性
    // class_copyIvarList:把成员属性列表复制一份给你
    // Ivar *:指向Ivar指针
    // Ivar *:指向一个成员变量数组
    // class:获取哪个类的成员属性列表
    // count:成员属性总数

    unsigned int count = 0;
    Ivar *ivarList = class_copyIvarList(self, &count);
    for (int i = 0; i < count; i++) {
        // 获取成员属性
        Ivar ivar = ivarList[i];
        // 获取成员名
        NSString *propertyName = [NSString stringWithUTF8String:ivar_getName(ivar)];
        //成员属性类型
        NSString *propertyType = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];
        //获取key
        NSString *key = [propertyName substringFromIndex:1];
        //获取字典的value
        id value = dict[key];
        // 给模型的属性赋值
        // value:字典的值
        // key:属性名

// user:NSDictionary
        //** '二级转换'**
          // 值是字典,成员属性的类型不是字典,才需要转换成模型

        if ([value isKindOfClass:[NSDictionary class]] && ![propertyType containsString:@"NS"]) {
            // 需要字典转换成模型
            // 转换成哪个类型

            // @"@\"User\"" User
            NSRange range = [propertyType rangeOfString:@"\""];
            propertyType = [propertyType substringFromIndex:range.location + range.length];
            // User\"";

            range = [propertyType rangeOfString:@"\""];
            propertyType = [propertyType substringFromIndex:range.location];
            // 字符串截取

            // 获取需要转换类的类对象
            Class modelClass = NSClassFromString(propertyType);
            if (modelClass) {
                value = [modelClass PH_modelWithDict:value];
            }

        }

<!-- 三级转换:NSArray中也是字典,把数组中的字典转换成模型.-->
        // 判断值是否是数组
        if ([value isKindOfClass:[NSArray class]]) {
            //判断对应类有没有实现字典数组转模型数组的协议
            if ([self respondsToSelector:@selector(PH_arrayContainModelClass)]) {

                // 转换成id类型,就能调用任何对象的方法
                id idSelf = self;

                // 获取数组中字典对应的模型
                NSString *type =  [idSelf PH_arrayContainModelClass][key];

                // 生成模型
                Class classModel = NSClassFromString(type);
                NSMutableArray *arrM = [NSMutableArray array];

                // 遍历字典数组,生成模型数组
                for (NSDictionary *dict in value) {
                    // 字典转模型
                    id model = [classModel PH_modelWithDict:dict];
                    [arrM addObject:model];
                }
                // 把模型数组赋值给value
                value = arrM;
            }
        }


        if (value) {
            // kvc赋值:不能传空
            [objc setValue:value forKey:key];
        }
        NSLog(@"%@",key);
        NSLog(@"%@,%@",propertyName,propertyType);
    }

    return objc;
}

3.转换

- (void)viewDidLoad {
    [super viewDidLoad];

    // 解析
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"status.plist" ofType:nil];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
    NSArray *dictArr = dict[@"statuses"];

    // 设计模型属性代码

    NSMutableArray *statuses = [NSMutableArray array];

    for (NSDictionary *dict in dictArr) {
        // 字典转模型
        Status *status = [Status PH_modelWithDict:dict];
        [statuses addObject:status];
    }
    NSLog(@"%@",statuses);
}

相关文章

  • iOS中字典转模型的方法

    作品链接:http://www.jianshu.com/users/1e0f5e6f73f6/top_articl...

  • 字典转模型

    字典转模型 1.老方法:按照字典中的key创建模型的属性,然后为模型创建一个方法,接收字典参数,在方法里进行属性赋...

  • Swift 字典转模型

    这里探讨字典转模型中模型的类型单一模型模型嵌套 (模型中包含模型 || 模型中包含模型数组) Swfit的字典转模...

  • 14-Swift中字典转模型

    字典转模型(初始化时传入字典) 字典转模型(利用KVC转化) 一、 普通的字典转模型: 二、利用KVC字典转模型:

  • iOS简单字典转模型

    使用方法:setValuesForKeysWithDictionary 进行字典转模型

  • iOS 使用Runtime机制将模型(对象)和字典相互转换

    在我们常见开发中往往需要将模型(对象)和字典相互转换,字典转模型(对象)相对简单,可以用系统方法快速实现,而模型(...

  • iOS-模型

    在开发中,经常使用到模型,通常做法就是字典转模型 字典转模型的过程最好封装在模型内部 模型应该提供一个可以传入字典...

  • iOS 字典转模型KVC实现

    字典转模型 KVC 实现 KVC 字典转模型弊端:必须保证,模型中的属性和字典中的key一一对应。 如果不一致,就...

  • iOS中字典转对象模型

    参考文章来自:http://www.cnblogs.com/fxiaoquan/p/4438126.html 有点...

  • Swift 5.0 使用MJExtension 字典转模型

    记录下swift怎么使用MJExtension 字典转模型1.普通的字典转模型 2.字典数组嵌套转模型

网友评论

  • iOS_愛OS:楼主,还是有时间 PH_arrayContainModelClass 提供下吧! 虽然我们都知道这个方法是怎么实现的,对于大部分新手来说缺了这个方法理解起来还是有困难的,楼主写的不错!
  • 1bba73694b20:你好,请问PH_arrayContainModelClass定义是什么呢?这篇文章写得真好.
    1bba73694b20:@打电话记错号码的人 嗯,不过我通过你写的注释,大概懂意思,我已经实现了,就是不知道与你是否一样
    打电话记错号码的人:@爱生活的翾 有些模型方法没有上传,只上传了一些主要代码,有时间上传。

本文标题:iOS中字典转模型的方法

本文链接:https://www.haomeiwen.com/subject/eiagjttx.html