Method * class_copyMethodList(Class _Nullable cls, unsigned int * _Nullable outCount)
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
unsigned methodCount = 0;
Method *methods = class_copyMethodList([self class], &methodCount);
for (NSInteger i = 0; i < methodCount; i++) {
Method method = methods[i];
NSLog(@"methodSEL = %@",NSStringFromSelector(method_getName(method)));
}
}
- (void)test {
}
+ (void)classMethod {
}
@end
可以看到打印为,
TESTRuntime[6187:276529] methodSEL = test
TESTRuntime[6187:276529] methodSEL = viewDidLoad
表明class_copyMethodList是可以获取一个类里面所有的已经实现了的对象方法
如果需要获取已经实现了的所有类方法, 通过object_getClass获取元类, 元类里面保存了所有的类方法
Method *methods = class_copyMethodList(object_getClass([self class]), &methodCount);
修改后打印为
TESTRuntime[6263:280454] methodSEL = classMethod









网友评论