福利->KVC+Runtime获取类/对象的属性/成员变量/方法/协议并实现字典转模型
我们知道,KVC+Runtime可以做非常多的事情。有了这个,我们可以实现很多的效果。
这里来个福利,利用KVC+Runtime获取类/对象的所有成员变量、属性、方法及协议;
并利用它来实现字典转模型。
废话不多说,直接上代码:
1、工具类(其实就是NSObject的一个分类)头文件
#import <Foundation/Foundation.h> @interface NSObject (YSRuntime) /**
返回当前类的属性数组 @return 属性数组(如:"name","age","address")
*/
+ (NSArray *)ys_propertiesList; /**
返回当前类的成员变量数组 @return 成员变量数组
*/
+ (NSArray *)ys_ivarsList; /**
返回当前类的对象方法数组 @return 方法数组
*/
+ (NSArray *)ys_methodList; /**
返回当前类的协议数组 @return 协议数组
*/
+ (NSArray *)ys_protocolList; /**
使用字典创建当前类的对象 @param dictionary 字典 @return 当前类的对象
*/
+ (instancetype)ys_objectWithDictionary:(NSDictionary *)dictionary; /**
使用字典数组创建当前类的对象数组 @param dictArray 字典数组 @return 当前类的对象数组
*/
+ (NSArray *)ys_objectsWithDictionaryArray:(NSArray<NSDictionary *> *)dictArray; @end
2、下面我们来实现里面的方法,以后就用它来获取类/对象的属性、成员变量、方法和协议
说明一下:最主要的方法是在objc/runtime.h,这里只是列举了一些,起到抛砖引玉的作用
#import "NSObject+YSRuntime.h"
#import <objc/runtime.h> @implementation NSObject (YSRuntime) #pragma mark - 属性数组
const char *propertiesKey = "ys.propertiesList";
+ (NSArray *)ys_propertiesList { // 获取关联对象
NSArray *result = objc_getAssociatedObject(self, propertiesKey); if (result != nil) {
return result;
} unsigned int count = ;
objc_property_t *list = class_copyPropertyList([self class], &count); NSMutableArray *arrayM = [NSMutableArray array]; for (unsigned int i = ; i < count; i++) { objc_property_t pty = list[i]; const char *cName = property_getName(pty);
NSString *name = [NSString stringWithUTF8String:cName]; [arrayM addObject:name];
} free(list); // 设置关联对象
objc_setAssociatedObject(self, propertiesKey, arrayM, OBJC_ASSOCIATION_COPY_NONATOMIC); return objc_getAssociatedObject(self, propertiesKey);
} #pragma mark - 私有方法,专门针对字典转模型中的自定义属性,{@"name":@"Dog"},name是属性名,Dog是自定义的模型类,属性名-属性类型
const char *propertiesTypeKey = "ys.propertiesTypeKey";
+ (NSArray<NSDictionary *> *)ys_propertiesAndTypeList { // 获取关联对象
NSArray *result = objc_getAssociatedObject(self, propertiesTypeKey); if (result != nil) {
return result;
} unsigned int count = ;
objc_property_t *list = class_copyPropertyList([self class], &count); NSMutableArray<NSDictionary *> *arrayM = [NSMutableArray<NSDictionary *> array]; for (unsigned int i = ; i < count; i++) { objc_property_t pty = list[i]; const char *cType = property_getAttributes(pty);
NSString *typeStr = [NSString stringWithUTF8String:cType]; if([typeStr containsString:@"\",&"]){
NSRange typeRange = [typeStr rangeOfString:@"\",&"];
NSString *type = [typeStr substringWithRange:NSMakeRange(, typeRange.location - )]; const char *cName = property_getName(pty);
NSString *name = [NSString stringWithUTF8String:cName]; NSDictionary *dict = @{name:type}; [arrayM addObject:dict];
}
} free(list); // 设置关联对象
objc_setAssociatedObject(self, propertiesTypeKey, arrayM, OBJC_ASSOCIATION_COPY_NONATOMIC); return objc_getAssociatedObject(self, propertiesTypeKey);
} #pragma mark - 成员变量数组
const char *ivarsKey = "ys.ivarsList";
+ (NSArray *)ys_ivarsList { // 获取关联对象
NSArray *result = objc_getAssociatedObject(self, ivarsKey); if (result != nil) {
return result;
} unsigned int count = ;
Ivar *list = class_copyIvarList([self class], &count); NSMutableArray *arrayM = [NSMutableArray array]; for (unsigned int i = ; i < count; i++) { Ivar ivar = list[i]; const char *cName = ivar_getName(ivar);
NSString *name = [NSString stringWithUTF8String:cName]; [arrayM addObject:name];
} free(list); // 设置关联对象
objc_setAssociatedObject(self, ivarsKey, arrayM, OBJC_ASSOCIATION_COPY_NONATOMIC); return objc_getAssociatedObject(self, ivarsKey);
} #pragma mark - 方法数组
+ (NSArray *)ys_methodList { unsigned int count = ;
Method *list = class_copyMethodList([self class], &count); NSMutableArray *arrayM = [NSMutableArray array]; for (unsigned int i = ; i < count; i++) { Method method = list[i]; SEL selector = method_getName(method);
NSString *name = NSStringFromSelector(selector); [arrayM addObject:name];
} free(list); return arrayM.copy;
} #pragma mark - 协议数组
+ (NSArray *)ys_protocolList { unsigned int count = ;
__unsafe_unretained Protocol **list = class_copyProtocolList([self class], &count); NSMutableArray *arrayM = [NSMutableArray array]; for (unsigned int i = ; i < count; i++) { Protocol *protocol = list[i]; const char *cName = protocol_getName(protocol);
NSString *name = [NSString stringWithUTF8String:cName]; [arrayM addObject:name];
} free(list); return arrayM.copy;
} #pragma mark - 字典 -> 当前类的对象
+ (instancetype)ys_objectWithDictionary:(NSDictionary *)dictionary{ // 当前类的属性数组
NSArray *list = [self ys_propertiesList]; NSArray<NSDictionary *> *propertyTypeList = [self ys_propertiesAndTypeList]; id obj = [self new]; for (NSString *key in dictionary) { if([list containsObject:key]){ if ([dictionary[key] isKindOfClass:[NSDictionary class]]){ // 属性值为字典 for(NSDictionary *dict in propertyTypeList){ if([key isEqualToString: [NSString stringWithFormat:@"%@",dict.allKeys.firstObject]]){ NSString *classTypeStr = dict[key];
Class class = NSClassFromString(classTypeStr); id objChild = [class ys_objectWithDictionary:dictionary[key]]; [obj setValue:objChild forKey:key];
}
} }
else if([dictionary[key] isKindOfClass:[NSArray<NSDictionary *> class]]){ // 属性值为字典数组 }
else{
[obj setValue:dictionary[key] forKey:key];
}
}
} return obj;
} #pragma mark - 字典数组 -> 当前类的对象数组
+ (NSArray *)ys_objectsWithDictionaryArray:(NSArray<NSDictionary *> *)dictArray { if (dictArray.count == ) {
return nil;
} // 当前类的属性数组
NSArray *list = [self ys_propertiesList]; NSArray<NSDictionary *> *propertyTypeList = [self ys_propertiesAndTypeList]; NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dictionary in dictArray) { id obj = [self new]; for (NSString *key in dictionary) { if([list containsObject:key]){ if ([dictionary[key] isKindOfClass:[NSDictionary class]]){ // 属性值为字典 for(NSDictionary *dict in propertyTypeList){ if([key isEqualToString: [NSString stringWithFormat:@"%@",dict.allKeys.firstObject]]){ NSString *classTypeStr = dict[key];
Class class = NSClassFromString(classTypeStr); id objChild = [class ys_objectWithDictionary:dictionary[key]]; [obj setValue:objChild forKey:key];
}
} }
else if([dictionary[key] isKindOfClass:[NSArray<NSDictionary *> class]]){ // 属性值为字典数组 }
else{
[obj setValue:dictionary[key] forKey:key];
}
}
} [arrayM addObject:obj];
} return arrayM.copy;
} @end
3、和YYModel一样,如果对象的一个属性为另一个自定义对象,那么同样可以起到连转的作用;
4、但比较遗憾的是,也是和YYModel一样,如果有一个属性是数组,又添加了泛型约束,没有取到这个数组的泛型约束。
我记得,YYModel就有这个问题,因为取不到泛型约束,所以当有属性为数组的时候,需要手动指定数组里面的元素类型。
希望各位大神看到后不吝赐教。
5、最后贴出一个小且非常有趣的小东东,里面的key就是我用上面的 “ys_ivarsList” 获取到所有成员变量,然后一级一级找的^_^
// 设置随机颜色给Application的statusBar,从此妈妈再也不用担心statusBar的背景色
id bgStyle = [[UIApplication sharedApplication] valueForKeyPath:@"statusBar.backgroundView.style"];
[bgStyle setValue:[UIColor redColor] forKey:@"backgroundColor"];
// frame = (5 649; 55 13)
// 修改高德地图和logo,删除和替换自己随意定
UIImageView *imgV = [mapView valueForKey:@"logoImageView"]; // 这就就是高德地图的logo
imgV.image = nil; // 我们把imageView的图片置为nil
[mapView setValue:imgV forKey:@"logoImageView"]; // 哈哈,这个高德地图的logo就不见了,妈妈从此再也不需要担心logo了
福利->KVC+Runtime获取类/对象的属性/成员变量/方法/协议并实现字典转模型的更多相关文章
- Runtime获取类的属性列表和方法列表
Runtime获取类的属性列表和方法列表 Runtime很强大,他使得OC中没有真正意义上的私有属性和私有方法,我们可以利用OC的运行时拿到一个类的任何方法和任何属性,然后动态的去调用方法,objc_ ...
- JAVA反射机制教程-获取类对象
1. 什么是类对象 类对象,就是用于描述这种类,都有什么属性,什么方法的 2. 获取类对象 获取类对象有3种方式(1). Class.forName(2). Hero.class(3). new He ...
- Python脚本控制的WebDriver 常用操作 <十七> 获取测试对象的属性及内容
测试用例场景 获取测试对象的内容是前端自动化测试里一定会使用到的技术.比如我们要判断页面上是否显示了一个提示,那么我们就需要找到这个提示对象,然后获取其中的文字,再跟我们的预期进行比较.在webdri ...
- WPFS数据绑定(要是后台类对象的属性值发生改变,通知在“client界面与之绑定的控件值”也发生改变须要实现INotitypropertyChanged接口)
WPFS数据绑定(要是后台类对象的属性值发生改变,通知在"client界面与之绑定的控件值"也发生改变须要实现INotitypropertyChanged接口) MainWindo ...
- 获取JSON对象的属性值
1.问题背景 有一个json对象,其中有键值对,那怎样获取json对象中属性值 2.实现源码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...
- 获取JSON对象的属性名称
1.问题背景 一个json对象,是以键值对组成,通过循环json对象,获取json对象中的属性名称 2.实现源码 <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- Python 获取类对象的父类
参考 Get parent class name? Python 获取类对象的父类 给定一个类的对象a,要求获取该对象的父类. 方法: a.__class__.__bases__ 返回由该对象的父类组 ...
- CWnd和HWND的区别(hWnd只是CWnd对象的一个成员变量,代表与这个对象绑定的窗口)
所有控件类都是CWnd类的派生类,CWnd的所有成员函数在控件类中都可以使用.在MFC中,CWnd类是一个很重要的类,它封装了Windows的窗口句柄HWND.在Windows编程中, ...
- 对List对象按照某个成员变量进行排序
/** * 对List对象按照某个成员变量进行排序 * @param list List对象 * @param sortField 排序的属性名称 * @param sortMode 排序方式:ASC ...
随机推荐
- Windows Azure Cloud Service (42) 使用Azure In-Role Cache缓存(1)Co-located Role
<Windows Azure Platform 系列文章目录> Update 2016-01-12 https://azure.microsoft.com/zh-cn/documentat ...
- Electro桌面应用开发之HelloWorld
简介 Electron (http://http://electron.atom.io)提供了一个使用Node.js进行桌面应用开发的环境. 本文介绍了一个基于Electron的HelloWorld ...
- C# 通过反射来动态创建泛型类型
C# 通过反射来动态创建泛型类型与创建普通类型的区别在于:泛型参数的处理 创建泛型类型存在三种情况: 第一种:知道泛型类型,但需要动态指定泛型参数: 第二种:知道泛型参数,但需要动态创建指定参数的泛型 ...
- swift3.0的改变
Swift在这2年的时间内,发展势头迅猛,在它开源后,更是如井喷一样,除了 iOS.mac 平台,还支持了 Linux. 而今年下半年, Swift 3.0 也会随之发布.https://github ...
- 模拟 --- hdu 12878 : Fun With Fractions
Fun With Fractions Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit ...
- web前端学习笔记---scrollWidth,clientWidth,offsetWidth的区别
通过一个demo测试这三个属性的差别. 说明: scrollWidth:对象的实际内容的宽度,不包边线宽度,会随对象中内容超过可视区后而变大. clientWidth:对象内容的可视区的宽度,不包滚动 ...
- Newtonsoft.Json 用法
Newtonsoft.Json 是.NET 下开源的json格式序列号和反序列化的类库.官方网站: http://json.codeplex.com/ 使用方法 1.首先下载你需要的版本,然后在应用程 ...
- php.ini 配置详细选项
php.ini 或 php3.ini 是 PHP 在启动时会读取的配置文件.该文件的存放路径为 /usr/local/lib/.在 PHP 3.x 版的配置文件为 php3.ini:而在 PHP 4. ...
- 把VIM打造成IDE
======================================= 安装vim相关软件 =============== ...
- volcanol_Linux_ 问题汇总系列_4_Thinkpad_E40_0578MDC_在Fedora 13 Linux(FC13)中如何安装无线网卡驱动
今天晚上,我突然想在自己到笔记本上安装linux系统,因为我自己第一次接触到的linux是红帽支持到Fedora Core 4,所以一直最中意的linux 发行版本是FC系列,同时由于FC 15以后到 ...