需要用到:
#import <objc/runtime.h>
在这里我们随便找一个控制器
@interface ViewController ()
@property(nonatomic,strong)NSString *name;
@end
//获取属性列表
unsigned int number;
objc_property_t *propertiList = class_copyPropertyList([self class], &number);
for (unsigned int i = 0; i < number; i++) {
const char *propertyName = property_getName(propertiList[i]);
NSLog(@"属性名称----%@",[NSString stringWithUTF8String:propertyName]);
}
free(propertiList);

//获取方法列表
unsigned int methodCount;
Method *method = class_copyMethodList([self class], &methodCount);
for (unsigned int i = 0; i < methodCount; i++) {
Method me = method[i];
NSLog(@"方法名称----%@",NSStringFromSelector(method_getName(me)));
}
free(method);

//获取成员变量列表
unsigned int ivarCount;
Ivar *ivar = class_copyIvarList([self class], &ivarCount);
for (NSInteger index = 0; index < ivarCount; index++) {
const char *ivarName = ivar_getName(ivar[index]);
NSLog(@"成员变量名称----%@",[NSString stringWithUTF8String:ivarName]);
}
free(ivar);

//获取协议列表

首先我们先遵守协议
@interface ViewController ()<NSCoding>
unsigned int protocalCount;
//获取协议列表
__unsafe_unretained Protocol **protocolList = class_copyProtocolList([self class], &protocalCount);
for (unsigned int i = 0; i< protocalCount; i++) {
Protocol *myProtocal = protocolList[i];
const char *protocolName = protocol_getName(myProtocal);
NSLog(@"协议名称----%@", [NSString stringWithUTF8String:protocolName]);
}
free(protocolList);

网友评论