我们知道,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获取类/对象的属性/成员变量/方法/协议并实现字典转模型的更多相关文章

  1. Runtime获取类的属性列表和方法列表

    Runtime获取类的属性列表和方法列表 Runtime很强大,他使得OC中没有真正意义上的私有属性和私有方法,我们可以利用OC的运行时拿到一个类的任何方法和任何属性,然后动态的去调用方法,objc_ ...

  2. JAVA反射机制教程-获取类对象

    1. 什么是类对象 类对象,就是用于描述这种类,都有什么属性,什么方法的 2. 获取类对象 获取类对象有3种方式(1). Class.forName(2). Hero.class(3). new He ...

  3. Python脚本控制的WebDriver 常用操作 <十七> 获取测试对象的属性及内容

    测试用例场景 获取测试对象的内容是前端自动化测试里一定会使用到的技术.比如我们要判断页面上是否显示了一个提示,那么我们就需要找到这个提示对象,然后获取其中的文字,再跟我们的预期进行比较.在webdri ...

  4. WPFS数据绑定(要是后台类对象的属性值发生改变,通知在“client界面与之绑定的控件值”也发生改变须要实现INotitypropertyChanged接口)

    WPFS数据绑定(要是后台类对象的属性值发生改变,通知在"client界面与之绑定的控件值"也发生改变须要实现INotitypropertyChanged接口) MainWindo ...

  5. 获取JSON对象的属性值

    1.问题背景 有一个json对象,其中有键值对,那怎样获取json对象中属性值 2.实现源码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...

  6. 获取JSON对象的属性名称

    1.问题背景 一个json对象,是以键值对组成,通过循环json对象,获取json对象中的属性名称 2.实现源码 <!DOCTYPE html PUBLIC "-//W3C//DTD ...

  7. Python 获取类对象的父类

    参考 Get parent class name? Python 获取类对象的父类 给定一个类的对象a,要求获取该对象的父类. 方法: a.__class__.__bases__ 返回由该对象的父类组 ...

  8. CWnd和HWND的区别(hWnd只是CWnd对象的一个成员变量,代表与这个对象绑定的窗口)

            所有控件类都是CWnd类的派生类,CWnd的所有成员函数在控件类中都可以使用.在MFC中,CWnd类是一个很重要的类,它封装了Windows的窗口句柄HWND.在Windows编程中, ...

  9. 对List对象按照某个成员变量进行排序

    /** * 对List对象按照某个成员变量进行排序 * @param list List对象 * @param sortField 排序的属性名称 * @param sortMode 排序方式:ASC ...

随机推荐

  1. 服务器程序DEBUG

    服务器程序DEBUG 服务器端设定 Tomcat 默认我们启动Tomcat是使用下边的命令 ./catalina.sh start 如果想DEBUG的话,只需要加一个参数打开JPDA(Java Pla ...

  2. vs2015使用GIt连接git.oschina.net/

    本文转自:http://www.bubuko.com/infodetail-1066588.html.谢谢作者 先安装Git命令行,下载地址:https://github.com/git-for-wi ...

  3. [UWP]涨姿势UWP源码——RSS feed的获取和解析

    本篇开始具体分析涨姿势UWP这个APP的代码,首先从数据的源头着手,即RSS feed的获取和解析,相关的类为RssReader,所有和数据相关的操作均放在里面. 涨姿势网站提供的RSS feed地址 ...

  4. child-selector解释

    这个伪类选择器应该叫孩子选择器,意思是选择网页中所有父节点的第一个子节点,并且这第一个子字节点必须是指定标签元素 写法有 :first-child :last-child :nth-child(odd ...

  5. char导致的验证异常

    表的一个字段: Moblie  char(15) 对应的mvc代码: @Html.EditorFor(c => c.Mobile) [RegularExpression("^1[3|4 ...

  6. ASP.NET MVC在布局页上使用模型(model)

    看到这标题有点怪,一般情况之下,我们很容易在视图与部分视图中使用模型(model),但是如果想在布局页_Layout.cshtml页中使用模型(model),按照普通方式也许没有达到预期的效果,在实现 ...

  7. ASP.NET Core实现OAuth2.0的AuthorizationCode模式

    前言 在上一篇中实现了resource owner password credentials和client credentials模式:http://www.cnblogs.com/skig/p/60 ...

  8. VS2015如何创建单元测试并启动调试

    1: 添加单元测试 2:打开单元测试类 关键点: 类上加上标记:[TestClass],方法上添加标记:[TestMethod],方法输出使用:Assert.IsNotNull(s,"测试失 ...

  9. Allok Video to FLV Converter 可以用的 FixFlash.exe

    纸飞机 拷至 c:\windows\system32 ok 下载链接:http://pan.baidu.com/s/1eQwz0DO 软件下载:http://pan.baidu.com/s/1sjGv ...

  10. Python 3 and MySQL

    http://stackoverflow.com/questions/4960048/python-3-and-mysql up vote61down votefavorite 20 I am usi ...