首页 > 其他 > 详细

runtime获取对象所有属性(变量)和方法

时间:2016-10-29 01:40:11      阅读:262      评论:0      收藏:0      [点我收藏+]

1、包含运行时头文件 

  <objc/runtime.h>

2、获取某个类的成员变量或则属性;

unsigned int numIvars; //成员变量个数

    Ivar *vars = class_copyIvarList(NSClassFromString(@"UIView"), &numIvars);

    //Ivar *vars = class_copyIvarList([UIView class], &numIvars);

    

    NSString *key=nil;

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


        Ivar thisIvar = vars[i];

        key = [NSString stringWithUTF8String:ivar_getName(thisIvar)];  //获取成员变量的名字

        NSLog(@"variable name :%@", key);

        key = [NSString stringWithUTF8String:ivar_getTypeEncoding(thisIvar)]; //获取成员变量的数据类型

        NSLog(@"variable type :%@", key);

    }

    free(vars);

 

3、获取成员函数

 

Method *meth = class_copyMethodList(NSClassFromString(@"UIView"), &numIvars);

    //Method *meth = class_copyMethodList([UIView class], &numIvars);

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

        Method thisIvar = meth[i];
        
        SEL sel = method_getName(thisIvar);

        const char *name = sel_getName(sel);


        NSLog(@"zp method :%s", name);

          }

    free(meth);

 

runtime获取对象所有属性(变量)和方法

原文:http://www.cnblogs.com/pengyunjing/p/6009702.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!