iOS 获取一个类的所有方法

2018-04-20  本文已影响0人  Winny_园球

import <objc/runtime.h>

import <objc/message.h>

需要导入运行时头文件和消息发送文件

- (void)runTests
{
    unsigned int count;
    Method *methods = class_copyMethodList([self class], &count);
    for (int i = 0; i < count; i++)
    {
        Method method = methods[i];
        SEL selector = method_getName(method);
        NSString *name = NSStringFromSelector(selector);

        if ([name hasPrefix:@"test"])
        NSLog(@"方法 名字 ==== %@",name);
        if (name)
        {
            //avoid arc warning by using c runtime
            objc_msgSend(self, selector);
        }
        NSLog(@"Test '%@' completed successfuly", [name substringFromIndex:4]);
    }
}
上一篇 下一篇

猜你喜欢

热点阅读