一、包含运行时头文件函数
#import <objc/runtime.h>this
二、获取某个类的成员变量或则属性;spa
unsigned int numIvars; //成员变量个数orm
Ivar *vars = class_copyIvarList(NSClassFromString(@"UIView"), &numIvars);get
//Ivar *vars = class_copyIvarList([UIView class], &numIvars);string
NSString *key=nil;it
for(int i = 0; i < numIvars; i++) {class
Ivar thisIvar = vars[i];import
key = [NSString stringWithUTF8String:ivar_getName(thisIvar)]; //获取成员变量的名字变量
NSLog(@"variable name :%@", key);
key = [NSString stringWithUTF8String:ivar_getTypeEncoding(thisIvar)]; //获取成员变量的数据类型
NSLog(@"variable type :%@", key);
}
free(vars);
三、获取成员函数
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);
例子中使用的UIView类能够替换成任何自定义的类。更多函数查看<objc/runtime.h> !