Runtime的使用3 【字典转模型】

转自:http://blog.csdn.net/wiki_su/article/details/75089174

提到字典转模型,MJExtension类似这个库,用过的就晓得转成model是多么的便捷。

本文就dictionary  -> model 略微介绍以下。


首先,创建一个model类。

[objc]  view plain  copy
  1. @interface Model : NSObject  
  2.   
  3. @property (copy,nonatomicNSString *name;  
  4. @property (copy,nonatomicNSString *sex;  
  5. @property (copy,nonatomicNSString *age;  
  6.   
  7. @end  

对于NSObject我们写个类别。添加一个方法。

[objc]  view plain  copy
  1. @interface NSObject (hook)  
  2.   
  3. + (instancetype)modelWithDict:(NSDictionary *)dict;  
  4.   
  5. @end  


然后实现这个方法。我们需要通过class_copyPropertyList来得到这个model所有的属性。然后通过

property来得到value。然后setValue对应起来二者的关系。

[objc]  view plain  copy
  1. #import "NSObject+hook.h"  
  2. #import <objc/runtime.h>  
  3.   
  4. @implementation NSObject (hook)  
  5.   
  6.   
  7. const charchar *kPropertyListKey = "YFPropertyListKey";  
  8.   
  9. + (NSArray *)yf_objcProperties  
  10. {  
  11.     /* 获取关联对象 */  
  12.     NSArray *ptyList = objc_getAssociatedObject(self, kPropertyListKey);  
  13.     /* 如果 ptyList 有值,直接返回 */  
  14.     if (ptyList) {  
  15.         return ptyList;  
  16.     }  
  17.     /* 调用运行时方法, 取得类的属性列表 */  
  18.     /* 成员变量: 
  19.      * class_copyIvarList(__unsafe_unretained Class cls, unsigned int *outCount) 
  20.      * 方法: 
  21.      * class_copyMethodList(__unsafe_unretained Class cls, unsigned int *outCount) 
  22.      * 属性: 
  23.      * class_copyPropertyList(__unsafe_unretained Class cls, unsigned int *outCount) 
  24.      * 协议: 
  25.      * class_copyProtocolList(__unsafe_unretained Class cls, unsigned int *outCount) 
  26.      */  
  27.     unsigned int outCount = 0;  
  28.     /** 
  29.      * 参数1: 要获取得类 
  30.      * 参数2: 类属性的个数指针 
  31.      * 返回值: 所有属性的数组, C 语言中,数组的名字,就是指向第一个元素的地址 
  32.      */  
  33.     /* retain, creat, copy 需要release */  
  34.     objc_property_t *propertyList = class_copyPropertyList([self class], &outCount);  
  35.     NSMutableArray *mtArray = [NSMutableArray array];  
  36.     /* 遍历所有属性 */  
  37.     for (unsigned int i = 0; i < outCount; i++) {  
  38.         /* 从数组中取得属性 */  
  39.         objc_property_t property = propertyList[i];  
  40.         /* 从 property 中获得属性名称 */  
  41.         const charchar *propertyName_C = property_getName(property);  
  42.         /* 将 C 字符串转化成 OC 字符串 */  
  43.         NSString *propertyName_OC = [NSString stringWithCString:propertyName_C encoding:NSUTF8StringEncoding];  
  44.         [mtArray addObject:propertyName_OC];  
  45.     }  
  46.     /* 设置关联对象 */  
  47.     /** 
  48.      *  参数1 : 对象self 
  49.      *  参数2 : 动态添加属性的 key 
  50.      *  参数3 : 动态添加属性值 
  51.      *  参数4 : 对象的引用关系 
  52.      */  
  53.     objc_setAssociatedObject(self, kPropertyListKey, mtArray.copy, OBJC_ASSOCIATION_RETAIN_NONATOMIC);  
  54.     /* 释放 */  
  55.     free(propertyList);  
  56.     return mtArray.copy;  
  57. }  
  58. + (instancetype)modelWithDict:(NSDictionary *)dict {  
  59.     /* 实例化对象 */  
  60.     id objc = [[self alloc]init];  
  61.     /* 使用字典,设置对象信息 */  
  62.     /* 1. 获得 self 的属性列表 */  
  63.     NSArray *propertyList = [self  yf_objcProperties];  
  64.     /* 2. 遍历字典 */  
  65.     [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOLBOOL * _Nonnull stop) {  
  66.         /* 3. 判断 key 是否字 propertyList 中 */  
  67.         if ([propertyList containsObject:key]) {  
  68.             /* 说明属性存在,可以使用 KVC 设置数值 */  
  69.             [objc setValue:obj forKey:key];  
  70.         }  
  71.     }];  
  72.     /* 返回对象 */  
  73.     return objc;  
  74. }  
  75.   
  76.   
  77. @end  

然后我们就可以通过dic转成model。用点语法来得到某个参数的值了。

[objc]  view plain  copy
  1. NSDictionary *dic = @{@"name":@"张三",  
  2.                         @"sex":@"男",  
  3.                         @"age":@25  
  4.                         };  
  5.   Model *model = [Model modelWithDict:dic];  
  6.   NSLog(@"name:%@  sex:%@  ",model.name,model.sex);  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值