代码之家  ›  专栏  ›  技术社区  ›  Dave DeLong

获取任意类的类方法列表

  •  13
  • Dave DeLong  · 技术社区  · 15 年前

    我怎样才能拿到 特定类的方法?我试过用 class_copyMethodList 函数在中声明 <objc/runtime.h> ,但这只给了我实例方法。我还找到了一个函数,它给我一个类方法的方法,但前提是我首先拥有方法的选择器。( class_getClassMethod )

    有什么想法吗?

    谢谢,

    戴夫

    2 回复  |  直到 8 年前
        1
  •  13
  •   Jim Correia    15 年前

    class_copyMethodList 返回所传递类的实例方法。类方法实际上是类的元类的实例方法。

    您的问题的解决方案包含在 API Documentation 对于 类\ CopyMethodList .

        2
  •  20
  •   Michael SarpErdag    8 年前

    使用元类。

    #import <objc/runtime.h>
    
    int unsigned numMethods;
    Method *methods = class_copyMethodList(objc_getMetaClass("NSArray"), &numMethods);
    for (int i = 0; i < numMethods; i++) {
        NSLog(@"%@", NSStringFromSelector(method_getName(methods[i])));
    }
    free(methods);