ios 上实现获取一个类中的所有方法以及属性
2017-08-21 本文已影响51人
helinyu
获取一个类中的所有方法
所有的属性应该也是类似的
NSArray* MethodsOfClass(Class cls){
NSMutableArray *methodObjs = @[].mutableCopy;
unsigned int methodCount = 0;
Method *methods = class_copyMethodList(cls, &methodCount);
if (methods) {
for (unsigned int i =0; i <methodCount; i++) {
SEL sel = method_getName(methods[i]);
const char *name = sel_getName(sel);
NSString *nameString ;
if (name) {
nameString = [NSString stringWithUTF8String:name];
}
YDClass *class = [YDClass new];
class.name = nameString;
class.sel = sel;
[methodObjs addObject:class];
}
}
return methodObjs;
}