美文网首页iOS
iOS 获取某个类里的所有方法名

iOS 获取某个类里的所有方法名

作者: 小子难缠 | 来源:发表于2022-03-12 18:59 被阅读0次

获取某个类里的所有方法名称

利用runtime获取方法名,首先需要导入runtime头文件 <objc/runtime.h>,以下是具体的代码实现:

/** 获取某个类里的所有方法名 cls:传真正的类名 */
- (void)printMethodNamesOfClass:(Class)cls{
    unsigned int count;
    /** 获得方法数组 */
    Method *methodList = class_copyMethodList(cls, &count);
    // 存储方法名
    NSMutableString *methodNames = [[NSMutableString alloc] init];
    /** 遍历所有的方法 */
    for (int i = 0; i < count; i++) {
        /** 获得方法名 */
        Method method = methodList[i];
        /** 获得方法名 */
        NSString *methodName = NSStringFromSelector(method_getName(method));
        /** 拼接方法名 */
        [methodNames appendFormat:@"%@, ",methodName];
    }
    /** 释放(因为是C语言函数,所以需要手动的内存管理,释放内存) */
    free(methodList);
    NSLog(@"%@ %@",cls,methodNames);
}

使用

[self printMethodNamesOfClass:object_getClass(self.person)];

相关文章

网友评论

    本文标题:iOS 获取某个类里的所有方法名

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