美文网首页
利用runtime 打印一个类中的所有方法名

利用runtime 打印一个类中的所有方法名

作者: KevinChein | 来源:发表于2018-10-01 08:37 被阅读23次
/**
 MJ: 利用runtime 打印方法名 #import <objc/runtime.h>

 @param clsObj 类对象或者元类对象   [Method class]/obj_getClass([Method class])
 */
- (void)printMethodListsOfClass:(Class) clsObj {
    // 1.获得方法数组
    unsigned int count;
    Method *methdList = class_copyMethodList(clsObj, &count);

    // 2.存储方法名
    NSMutableString *methodNames = [NSMutableString string];

    // 3.遍历所有的方法
    for (int i = 0; i < count; i++) {
        // 3.1 获得方法
        Method methodSelector= methdList[count];
        // 3.2 获得方法名
        NSString *methodName = NSStringFromSelector(method_getName(methodSelector));
        // 3.3 拼接方法名--存储
        [methodNames appendString:methodName];
        [methodNames appendString:@","];
    }

    // 4.释放
    free(methdList);

    // 5.打印方法名
    NSLog(@"%@----%@",clsObj, methodNames);
}

runtime打印方法名.png

相关文章

网友评论

      本文标题:利用runtime 打印一个类中的所有方法名

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