一、 获取方法列表----class_copyMethodList
method_getName、sel_getName
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];
}
[methodObjs addObject:nameString];
}
}
return methodObjs;
}
二、 获取属性列表 class_copyIvarList
ivar_getName、ivar_getTypeEncoding
- (void)iVarGetClassNameList:(Class)cls{
unsigned int outCount; // 1
Ivar *ivars = class_copyIvarList(cls, &outCount); // 2
for (int i = 0; i < outCount; i++) { // 3
Ivar ivar = ivars[i]; // 4
const char *ivarName = ivar_getName(ivar); // 5
const char *ivarType = ivar_getTypeEncoding(ivar); // 6
NSLog(@"实例变量名为:%s 字符串类型为:%s", ivarName,ivarType); // 7
} // 8
free(ivars);
}