Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Any ideas?

Thanks,

Dave

like image 813
Dave DeLong Avatar asked Aug 15 '09 18:08

Dave DeLong


People also ask

How do you find the methods of a class?

The getMethods() method of java. lang. Class class is used to get the methods of this class, which are the methods that are public and its members or the members of its member classes and interfaces. The method returns the methods of this class in the form of an array of Method objects.

How do you get all the methods of a Python object?

The simplest way to get a list of methods of any object is to use the help() command. It will list out all the available/important methods associated with that object.

How many methods of class are there?

There are three main types of methods: interface methods, constructor methods, and implementation methods.


1 Answers

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);
like image 137
user96459 Avatar answered Oct 29 '22 10:10

user96459