Runtime的使用3 【字典转模型】
来源:互联网 发布:linux 软阵列 编辑:程序博客网 时间:2024/06/05 10:17
提到字典转模型,MJExtension类似这个库,用过的就晓得转成model是多么的便捷。
本文就dictionary -> model 略微介绍以下。
首先,创建一个model类。
@interface Model : NSObject@property (copy,nonatomic) NSString *name;@property (copy,nonatomic) NSString *sex;@property (copy,nonatomic) NSString *age;@end
对于NSObject我们写个类别。添加一个方法。
@interface NSObject (hook)+ (instancetype)modelWithDict:(NSDictionary *)dict;@end
然后实现这个方法。我们需要通过class_copyPropertyList来得到这个model所有的属性。然后通过
property来得到value。然后setValue对应起来二者的关系。
#import "NSObject+hook.h"#import <objc/runtime.h>@implementation NSObject (hook)const char *kPropertyListKey = "YFPropertyListKey";+ (NSArray *)yf_objcProperties{ /* 获取关联对象 */ NSArray *ptyList = objc_getAssociatedObject(self, kPropertyListKey); /* 如果 ptyList 有值,直接返回 */ if (ptyList) { return ptyList; } /* 调用运行时方法, 取得类的属性列表 */ /* 成员变量: * class_copyIvarList(__unsafe_unretained Class cls, unsigned int *outCount) * 方法: * class_copyMethodList(__unsafe_unretained Class cls, unsigned int *outCount) * 属性: * class_copyPropertyList(__unsafe_unretained Class cls, unsigned int *outCount) * 协议: * class_copyProtocolList(__unsafe_unretained Class cls, unsigned int *outCount) */ unsigned int outCount = 0; /** * 参数1: 要获取得类 * 参数2: 类属性的个数指针 * 返回值: 所有属性的数组, C 语言中,数组的名字,就是指向第一个元素的地址 */ /* retain, creat, copy 需要release */ objc_property_t *propertyList = class_copyPropertyList([self class], &outCount); NSMutableArray *mtArray = [NSMutableArray array]; /* 遍历所有属性 */ for (unsigned int i = 0; i < outCount; i++) { /* 从数组中取得属性 */ objc_property_t property = propertyList[i]; /* 从 property 中获得属性名称 */ const char *propertyName_C = property_getName(property); /* 将 C 字符串转化成 OC 字符串 */ NSString *propertyName_OC = [NSString stringWithCString:propertyName_C encoding:NSUTF8StringEncoding]; [mtArray addObject:propertyName_OC]; } /* 设置关联对象 */ /** * 参数1 : 对象self * 参数2 : 动态添加属性的 key * 参数3 : 动态添加属性值 * 参数4 : 对象的引用关系 */ objc_setAssociatedObject(self, kPropertyListKey, mtArray.copy, OBJC_ASSOCIATION_RETAIN_NONATOMIC); /* 释放 */ free(propertyList); return mtArray.copy;}+ (instancetype)modelWithDict:(NSDictionary *)dict { /* 实例化对象 */ id objc = [[self alloc]init]; /* 使用字典,设置对象信息 */ /* 1. 获得 self 的属性列表 */ NSArray *propertyList = [self yf_objcProperties]; /* 2. 遍历字典 */ [dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { /* 3. 判断 key 是否字 propertyList 中 */ if ([propertyList containsObject:key]) { /* 说明属性存在,可以使用 KVC 设置数值 */ [objc setValue:obj forKey:key]; } }]; /* 返回对象 */ return objc;}@end
然后我们就可以通过dic转成model。用点语法来得到某个参数的值了。
NSDictionary *dic = @{@"name":@"张三", @"sex":@"男", @"age":@25 }; Model *model = [Model modelWithDict:dic]; NSLog(@"name:%@ sex:%@ ",model.name,model.sex);
阅读全文
0 0
- Runtime的使用3 【字典转模型】
- Runtime的使用3 【字典转模型】
- MJExtention 字典转模型详解使用注意以及利用runtime字典转模型的使用注意
- Runtime运行时的简单使用,字典转模型
- iOS runtime字典转模型
- runtime -- 实现字典转模型
- Runtime的入门与应用之八-字典转模型
- runtime从入门到精通(八)—— 使用runtime实现字典转模型
- runtime模型与字典互转
- KVC和runtime字典转模型
- Runtime(字典转模型)学习
- XZ_iOS之Runtime使用运行时实现字典转模型和关联对象
- Runtime 解析字典自动生成属性代码 及 字典转模型方式一 KVC/Runtime
- swift之用runtime实现字典转模型
- MJExtension 2.0 字典转模型再叙 常用的字典转模型使用实例说明
- 27-UI基础字典转模型与xib的使用
- 字典转模型的例子
- 字典转模型的方法
- b/s 和 c/s的区别
- 关于laravel的log组件使用
- 复杂链表复制
- 大一课程设计(TTMS单机版)
- 通过layer实现可输入的模态框
- Runtime的使用3 【字典转模型】
- python单元测试之unittest框架使用总结
- Android的高德地图实现行政区域显示
- tensorboard无法使用以及对应网址无法打开
- Java-json对象转Map
- CodeForces 106C Buns 多重背包
- javascript原型与原型链
- spring+hibernate整合时出现的错误
- 5-14 求整数段和 (15分)