获取任意类的类方法列表

[英]Obtain list of class methods for an arbitrary class


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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2009/08/15/c4246d624d4119ce18d505793f41ace7.html



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