美文网首页
runtime - class_copyMethodList

runtime - class_copyMethodList

作者: _RG | 来源:发表于2019-08-23 15:38 被阅读0次
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

相关文章

网友评论

      本文标题:runtime - class_copyMethodList

      本文链接:https://www.haomeiwen.com/subject/cghtectx.html