[翻译]  Obtain list of class methods for an arbitrary class

[CHINESE]  获取任意类的类方法列表


How can I get the list of class methods for a particular Class? I've tried using the class_copyMethodList function declared in <objc/runtime.h>, but that's only giving me instance methods. I've also found a function that gives me a Method for a class method, but only if I have the selector to the method first (class_getClassMethod).

如何获取特定类的类方法列表?我已经尝试使用在 中声明的class_copyMethodList函数,但这只是给我实例方法。我还找到了一个函数,它为我提供了一个类方法的方法,但前提是我首先拥有方法的选择器(class_getClassMethod)。

Any ideas?

有任何想法吗?

Thanks,

谢谢,

Dave

戴夫

2 个解决方案

#1


13  

class_copyMethodList returns the instance methods of the passed class. The class methods are actually instance methods of the class's metaclass.

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

The solution to your problem is included right in the API Documentation for class_copyMethodList.

您的问题的解决方案包含在class_copyMethodList的API文档中。

#2


20  

Use the metaclass.

使用元类。

#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);

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号