美文网首页
获取一个类的所有属性和方法

获取一个类的所有属性和方法

作者: 吓死宝宝了啊 | 来源:发表于2017-07-22 16:38 被阅读24次

调用下面两个方法必须引入#impot<objc/runtime.h>

1.获取所有的属性

- (NSArray *)getClassAttribute:(id)class

{

unsigned int count;

objc_property_t *properties = class_copyPropertyList([class class], &count);

NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count];

for(int i = 0; i < count; i++)

{

const char  *propertyName = property_getName(properties[i]);

[propertiesArray addObject: [NSString stringWithUTF8String:propertyName]];

}

free(properties);

return propertiesArray;

}

2. 获取所有的方法

- (void)getAllMethodsFromClass:(id)obj {

u_int count;

Method * methList = class_copyMethodList([obj class], &count);//获取类的方法列表

for (int i = 0; i < count; i++) {

Method temp = methList[i];

//        IMP imp = method_getImplementation(temp);//有Method获取IMP指针

SEL name = method_getName(temp);//由Method获取SEL

const char * name_s = sel_getName(name);

int arguments = method_getNumberOfArguments(temp);//获取参数个数

const char * encoding = method_getTypeEncoding(temp);//有Method获取Encoding类型

NSLog(@"方法名:%@\n,参数个数:%d\n,编码方式:%@\n",[NSString stringWithUTF8String:name_s],

arguments,[NSString stringWithUTF8String:encoding]);

}

free(methList);

}

相关文章

  • Runtime 那些事儿 二

    1. 获取一个类的所有方法 2. 获取一个类的所有属性 3. 获取/设置类的属性变量 4. 判断类的某个属性的类型...

  • java基础类-6-反射

    常用类 获取类类型 创建对象 获取属性 获取特定属性 方法和构造方法

  • Runtime相关函数 class_copyMethodList

    class_copyMethodList 实现一个类,定义属性,成员变量,实例方法,类方法:如下 获取一个类的所有...

  • JAVA反射总结

    概念 在运行状态中,对于任意一个类,都能获取类的所有方法和属性;对应任意一个对象,都能调用它的任意一个方法和属性。...

  • 反射基础

    反射 反射能在运行时获取任意一个类的所有属性和方法,前提是他能获取到类的Class对象

  • python学习笔记-(4)面向对象

    类定义格式 添加和获取对象属性 类外面添加对象属性 类外面获取对象属性 类里面获取对象属性 魔法方法 __init...

  • 反射

    实体类 反射获取实例 反射获取方法 反射获取属性 补充 testBean.getClasses()返回调用类的所有...

  • 获取一个类的所有属性和方法

    调用下面两个方法必须引入#impot 1.获取所有的属性 - (NSArray *...

  • swift 获取某个类的所有属性

    获取某个类的所有属性,这里举例UIAlertAction 调用下面方法打印出属性列表

  • 2019 面试前的准备 -- runtime

    runtime 笔记 获取类的所有属性和变量 class_copyIvarList 输出结果: 获取所有属性 cl...

网友评论

      本文标题:获取一个类的所有属性和方法

      本文链接:https://www.haomeiwen.com/subject/qefukxtx.html