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

Runtime很强大,他使得OC中没有真正意义上的私有属性和私有方法,我们可以利用OC的运行时拿到一个类的任何方法和任何属性,然后动态的去调用方法,objc_megsend(),甚至可以在运行时动态的为一个类去添加属性和方法,此篇博客要学习的是两个知识点:

  • 获取对象的所有属性 
  • 获取对象的所有方法

为了方便,我们可以在项目中为NSObject添加一个category,增加下面两个方法,这样我们就可以轻轻松松获得每个类的所以方法和所以属性了,记得导入runtime的头文件哦。

获取对象的所有属性、方法、属性和属性内容

NSObject+JGRuntime.h文件

#import <Foundation/Foundation.h>

@interface NSObject (JGRuntime)

/* 获取对象的所有属性 */
+(NSArray *)getAllProperties; /* 获取对象的所有方法 */
+(NSArray *)getAllMethods; /* 获取对象的所有属性和属性内容 */
+ (NSDictionary *)getAllPropertiesAndVaules; @end

NSObject+JGRuntime.m文件

#import "NSObject+JGRuntime.h"
#import <objc/runtime.h> @implementation NSObject (JGRuntime) /* 获取对象的所有属性 */
+(NSArray *)getAllProperties
{
u_int count;
// 传递count的地址过去 &count
objc_property_t *properties =class_copyPropertyList([self class], &count);
//arrayWithCapacity的效率稍微高那么一丢丢
NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count]; for (int i = 0; i < count ; i++)
{
//此刻得到的propertyName为c语言的字符串
const char* propertyName =property_getName(properties[i]);
//此步骤把c语言的字符串转换为OC的NSString
[propertiesArray addObject: [NSString stringWithUTF8String: propertyName]];
}
//class_copyPropertyList底层为C语言,所以我们一定要记得释放properties
// You must free the array with free().
free(properties); return propertiesArray;
} /* 获取对象的所有方法 */
+(NSArray *)getAllMethods
{
unsigned int methodCount =0;
Method* methodList = class_copyMethodList([self class],&methodCount);
NSMutableArray *methodsArray = [NSMutableArray arrayWithCapacity:methodCount]; for(int i=0;i<methodCount;i++)
{
Method temp = methodList[i];
IMP imp = method_getImplementation(temp);
SEL name_f = method_getName(temp);
const char* name_s =sel_getName(method_getName(temp));
int arguments = method_getNumberOfArguments(temp);
const char* encoding =method_getTypeEncoding(temp);
NSLog(@"方法名:%@,参数个数:%d,编码方式:%@",[NSString stringWithUTF8String:name_s],
arguments,
[NSString stringWithUTF8String:encoding]);
[methodsArray addObject:[NSString stringWithUTF8String:name_s]];
}
free(methodList);
return methodsArray;
} /* 获取对象的所有属性和属性内容 */
+ (NSDictionary *)getAllPropertiesAndVaules
{
NSMutableDictionary *propsDic = [NSMutableDictionary dictionary];
unsigned int outCount;
objc_property_t *properties =class_copyPropertyList([self class], &outCount);
for ( int i = 0; i<outCount; i++)
{
objc_property_t property = properties[i];
const char* char_f =property_getName(property);
NSString *propertyName = [NSString stringWithUTF8String:char_f];
id propertyValue = [self valueForKey:(NSString *)propertyName];
if (propertyValue) {
[propsDic setObject:propertyValue forKey:propertyName];
}
}
free(properties);
return propsDic;
} @end

调用时:

包含头文件

#import "NSObject+JGRuntime.h"

直接调用

如我在一个控制器的导航栏按钮点击时调用时

JGLog(@"\n%@",[JGWorkLogViewController getAllMethods]);

打印结果是:

(

setupChildVces,

setupTitlesView,

setupContentView,

titleClick:,

setTitlesView:,

setIndicatorView:,

setSelectedButton:,

indicatorView,

selectedButton,

titlesView,

setNavItem,

rightItemBtnClick,

leftitemBtnClick,

loadCtrlWithLogTypeStr:,

setContentView:,

scrollViewDidEndDecelerating:,

scrollViewDidEndScrollingAnimation:,

didReceiveMemoryWarning,

viewDidLoad,

.cxx_destruct,

contentView,

)

Runtime获取类的属性列表和方法列表的更多相关文章

  1. 反射,获取类的属性以及get方法

    vo实体类: public class Result { /*** * 返回结果code * ok:10000 * error:20000 */ private String code; /*** * ...

  2. 福利->KVC+Runtime获取类/对象的属性/成员变量/方法/协议并实现字典转模型

    我们知道,KVC+Runtime可以做非常多的事情.有了这个,我们可以实现很多的效果. 这里来个福利,利用KVC+Runtime获取类/对象的所有成员变量.属性.方法及协议: 并利用它来实现字典转模型 ...

  3. Java反射学习-1 - 反射获取类的属性,方法,构造器

    新建一个Person类 package cn.tx.reflect; /** * 注解初步了解 * @author Administrator * */ public class Person { p ...

  4. Python基础:面向对象基础 (一) 类及其属性和魔法方法

    定义类,添加和获取对象属性 # 定义类 格式如下 # class 类名: # 方法列表 # 新式类定义形式 # info 是一个实例方法,第一个参数一般是self,表示实例对象本身 class Her ...

  5. java利用反射获取类的属性及类型

    java利用反射获取类的属性及类型. import java.lang.reflect.Field; import java.math.BigDecimal; import java.util.Map ...

  6. C#获取当前堆栈的各调用方法列表

    在使用.NET编写的代码在debug时很容易进行排查和定位问题,一旦项目上线并出现问题的话那么只能依靠系统日志来进行问题排查和定位,但当项目复杂时,即各种方法间相互调用将导致要获取具体的出错方法或调用 ...

  7. 如何获取类或属性的自定义特性(Attribute)

    如何获取类或属性的自定义特性(Attribute) 问题说明: 在ActiveRecord或者其他的ORM等代码中, 我们经常可以看到自定义特性(Attribute)的存在(如下面的代码所示) [Pr ...

  8. .NET C#利用反射获取类文件以及其中的方法&属性 并获取类及方法上的特性

    了解C#特性类并声明我们自己的特性类[AttributeTest]代码如下 using System; namespace AttributeTest { /* 特性说明 特性本质是一个继承和使用了系 ...

  9. swift3.0 运行时获取类的属性

    //定义Person类 class Person: NSObject { var name: String? //注意这里基本数据类型我定义的是必选属性 var age: Int = override ...

随机推荐

  1. hibernate的面向对象查询的效率有多低?

    前言 老平台的查询速度很慢,需要进行优化(...说白了就是优化sql语句),老平台用的strus2+hibernate框架,查询基本都是使用的HSQL. 关于hsql HQL是Hibernate Qu ...

  2. Sublime text3:安装插件SublimeREPL解决不支持input

    Sublime text3:安装插件SublimeREPL解决不支持input 安装SublimeREPL 1,调用ctrl+shift+p 输入install回车: 2,输出:sublimerepl ...

  3. MVC readioButtonList的创作过程及运用

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Li ...

  4. 20145103《JAVA程序设计》第十周学习总结

    网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的数据,这个就是狭义的网络编程范畴.在发送和接收数据时,大部分的程序设 ...

  5. C++DFS方法全排列

    前几天看纪磊的<啊哈!算法>一书,里面讲算法讲的特别通俗细致,真的是初中生都能读得懂的算法书(我大二才读:P).这段代码很适合初学算法的同学. #include<iostream&g ...

  6. 求CRC16校验

    unsigned short DialogSerial::crc_ccitt(unsigned char *q,int len){ unsigned short ccitt_table[256] = ...

  7. Ubuntu 16.04 + Nvidia 显卡驱动 + Cuda 8.0 (问题总结 + 解决方案)【转】

    本文转载自:https://blog.csdn.net/Zafir_410/article/details/73188228 前言 前面好一阵子忙于写论文和改论文,好久没有做新实验了,最近又回到做实验 ...

  8. 混合开发的大趋势之一React Native之简单的登录界面

    转载请注明出处:王亟亟的大牛之路 这些天都在学习RN这部分吧,然后写了个简单的登陆业务,从"实战"中讲解吧 还是继续安利:https://github.com/ddwhan0123 ...

  9. Gym100783C Golf Bot(FFT)

    https://vjudge.net/problem/Gym-100783C 题意: 给出n个数,然后有m次查询,每次输入一个数x,问x能否由n个数中2个及2个以下的数相加组成. 思路:题意很简单,但 ...

  10. 监控控制台是否运行的bat

    @echo offrem set secs=5set srvname="TRS.Export.Scheduler.exe" echo.echo ================== ...