第02天实战技术(03):Runtime(字典转模型,一级转换)
2017-03-25 00:00
453 查看
#####一、使用KVC进行字典转模型的弊端
#####二、 runtime的小知识点
1.runtime字典转模型的一些方法
2.带指针得函数
3.runtime获取成员变量的数组需要用一个指针来获取
4.
#####三、(重点)使用RunTime进行字典转模型
NSObject+Model(为
功能不够强大 比如模型里面有字典(我们有需要通过字典转模型),但是KVC是不知道的, KVC也是可以做的,需要单独处理 .h @interface StatusModel : NSObject @property(nonatomic, strong) NSString * source; @property(nonatomic, assign) NSInteger reposts_count; @property(nonatomic, strong) NSArray * pic_urls; @property(nonatomic, strong) NSString * created_at; @property(nonatomic, assign) BOOL isA; @property(nonatomic, assign) NSInteger attitudes_count; @property(nonatomic, strong) NSString * idstr; @property(nonatomic, strong) NSString * text; @property(nonatomic, assign) NSInteger comments_count; @property(nonatomic, strong) User * user; + (instancetype)modelWithDict:(NSDictionary *)dict; @end .m #pragma 模型里面包含模型 使用KVC处理,需要单独做处理 - (void)setUser:(NSDictionary *)user { _user = }
#####二、 runtime的小知识点
1.runtime字典转模型的一些方法
参数 // <#__unsafe_unretained Class cls#> 获取哪个类的成员变量 // <#unsigned int *outCount#> 成员变量的总数 >>>> // 获取类里面的所有方法 (copy的好处就是 避免修改系统里面的东西) // class_copyMethodList(<#__unsafe_unretained Class cls#>, <#unsigned int *outCount#>) // 获取类里面的属性 // Property : 属性 // class_copyPropertyList(<#__unsafe_unretained Class cls#>, <#unsigned int *outCount#>) 比较少用这个 // 获取类里面的成员属性 // Ivar : 成员变量 以下划线开头 // class_copyIvarList(<#__unsafe_unretained Class cls#>, <#unsigned int *outCount#>) *** 开发者最常用这个
2.带指针得函数
#pragma mark 函数 // 带有指针的函数 void test (int *count) { *count = 3; }
3.runtime获取成员变量的数组需要用一个指针来获取
/* OBJC_EXPORT Ivar *class_copyIvarList(Class cls, unsigned int *outCount) OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); Ivar * 返回一个数组 例子: int a = 2; int b = 3; int c = 4; int arr[] = {a,b,c}; int *p = arr; NSLog(@"%d %d",p[0],p[1]); (int *) p = 0x00007fff5de868ec */
4.
成员变量与
属性的区别
#pragma mark 成员变量 与 属性的 区别 /** 成员变量 是以 _开头的 { int _a; } @property(nonatomic, assign) NSInteger reposts_count; // reposts_count 属性名字 _reposts_count 成员变量 为什么要用 Ivar 因为我们使用成员变量不会漏掉属性,但是我们用属性 可能会漏掉成员变量 */
#####三、(重点)使用RunTime进行字典转模型
模型是一个对象,需要为NSObject添加一个分类(用来字典转模型的) 使用方式
NSObject+Model(为
字典转模型的
NSObject扩充一个
分类)
// 本质 : 创建谁的对象 + (instancetype)modelWithDictionary:(NSDictionary *)dict { // 使用runtime实现 字典转模型 // 1.获取对象 // 2. 把字典中所有的值 给模型赋值 (正常的想法) id objc = [[self alloc]init]; // runtime : 根据模型中属性,去字典中取出对应的value给模型属性赋值 (runtime想法) // 1.获取模型中所有成员变量 key // <#__unsafe_unretained Class cls#> 获取哪个类的成员变量 // <#unsigned int *outCount#> 成员变量的总数 unsigned count = 0; // 获取成员变量数组 (class_copyIvarList) Ivar *ivarList = class_copyIvarList(self, &count); // Ivar ivar = ivarList[0]; // 取出是成员变量 // // 获取成员变量名字 // NSString *ivarName = [NSString stringWithUTF8String:ivar_getName(ivar)]; // 并且将C的字符串 转换成 OC的名字 // 遍历所有的成员变量 for (int i = 0 ; i<count; i++) { Ivar ivar = ivarList[i]; // 取出是成员变量 // 获取成员变量名字 NSString *ivarName = [NSString stringWithUTF8String:ivar_getName(ivar)]; // 并且将C的字符串 转换成 OC的名字 NSLog(@"%@",ivarName); // 获取key // 裁剪第1位 NSString *key = [ivarName substringFromIndex:1]; // NSLog(@"key : %@",key); // 2.根据属性名去字典中查找 value id value = dict[key]; // 3.给模型中属性赋值 KVC // 判断有值才进行赋值 if (value) { [objc setValue:value forKey:key]; } } // test(&count); NSLog(@"%i",count); return objc; }
#import "ViewController.h" #import "NSDictionary+Property.h" #import "StatusModel.h" #import "NSObject+Model.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 文件的全路径 // 1.获取文件的全路径 NSString *path = [[NSBundle mainBundle]pathForResource:@"status.plist" ofType:nil]; // 2.生成了一个字典 NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; NSLog(@" -- %@",dict); // 设置模型, 创建属性代码 => dict // [dict createPropertyCode]; // 3.字典转模型 StatusModel *model = [StatusModel modelWithDictionary:dict]; } @end
相关文章推荐
- 第02天实战技术(04):runtime字典转模型
- iOS开发——高级技术精选OC篇&Runtime之字典转模型实战
- 第02天实战技术(02):KVC字典转模型
- runtime最后一章 --- 接上昨晚字典转模型进行二级转换
- 第02天实战技术(01):runtime回顾
- Runtime 一一 runtime实现字典转模型,多级转换.
- Runtime 解析字典自动生成属性代码 及 字典转模型方式一 KVC/Runtime
- Runtime - 运行时 理解和应用 字典转模型框架 AFN中的交换方法
- runtime -- 实现字典转模型
- runtime从入门到精通(八)—— 使用runtime实现字典转模型
- 利用runtime实现字典转模型
- 关于 KVO 和 KVC 的一些总结 以及使用KVC进行字典和模型的互相转换
- OC字典转模型和数据列表一级缓存
- IPhone项目 字典,数组,服务器返回的json 一键转换数据模型的.h .m文件
- MJExtension字典和模型之间相互转换的轻量级框架的使用实例
- swift之用runtime实现字典转模型
- 详解iOS应用UI开发中的九宫格坐标计算与字典转换模型
- IOS 字典快速转换为Model 模型
- 字典(JSON)与模型的转换
- Mantle--国外程序员最常用的iOS模型&字典转换框架