44

In Objective-C I can test whether a given class or instance responds to certain selectors. But how can query a class or instance for all its methods or properties of a class (e.g. a list of all methods or properties)?

4 Answers 4

60

In addition to Buzzy's answer, for debugging purposes you may use the -[NSObject _methodDescription] private method.

Either in lldb:

(lldb) po [[UIApplication sharedApplication] _methodDescription]

or in code:

@interface NSObject (Private)
- (NSString*)_methodDescription;
@end

// Somewhere in the code:
NSLog(@"%@", [objectToInspect performSelector:@selector(_methodDescription)]);

Output will look as following:

<__NSArrayM: 0x7f9 ddc4359a0>:
in __NSArrayM:
    Class Methods:
        + (BOOL) automaticallyNotifiesObserversForKey:(id)arg1; (0x11503b510)
        + (id) allocWithZone:(_NSZone*)arg1; (0x11503b520)
        + (id) __new:::::(const id*)arg1; (0x114f0d700)
    Instance Methods:
        - (void) removeLastObject; (0x114f669a0)
        - (void) dealloc; (0x114f2a8f0)
        - (void) finalize; (0x11503b2c0)
        - (id) copyWithZone:(_NSZone*)arg1; (0x114f35500)
        - (unsigned long) count; (0x114f0d920)
        - (id) objectAtIndex:(unsigned long)arg1; (0x114f2a730)
        - (void) getObjects:(id*)arg1 range:(_NSRange)arg2; (0x114f35650)
        - (void) addObject:(id)arg1; (0x114f0d8e0)
        - (void) setObject:(id)arg1 atIndex:(unsigned long)arg2; (0x114f99680)
        - (void) insertObject:(id)arg1 atIndex:(unsigned long)arg2; (0x114f0d940)
        - (void) exchangeObjectAtIndex:(unsigned long)arg1 withObjectAtIndex:(unsigned long)arg2; (0x114f8bf80)
        ......
in NSMutableArray:
    Class Methods:
        + (id) copyNonRetainingArray; (0x11ee20178)
        + (id) nonRetainingArray; (0x11ee201e8)
        + (id) nonRetainingArray; (0x120475026)
        + (id) arrayWithCapacity:(unsigned long)arg1; (0x114f74280)
        ......
3
  • 3
    Best answer so far
    – mafonya
    Nov 27, 2017 at 16:17
  • 2
    It looks like this method has been renamed to fp_methodDescription (also fp_shortMethodDescription and fp_ivarDescription).
    – DarkDust
    Oct 12, 2022 at 9:18
  • fp_methodDescription lists inherited methods too, up to and including NSObject. fp_shortMethodDescription does not list inherited methods. Nov 13, 2022 at 13:33
50

You can do this and it is extremely well documented at https://developer.apple.com/library/mac/documentation/cocoa/Reference/ObjCRuntimeRef/index.html

To fetch all the instance or class methods of a class, you may use class_copyMethodList and iterate over the results. An example:

 #import <objc/runtime.h>

/**
 *  Gets a list of all methods on a class (or metaclass)
 *  and dumps some properties of each
 *
 *  @param clz the class or metaclass to investigate
 */
void DumpObjcMethods(Class clz) {

    unsigned int methodCount = 0;
    Method *methods = class_copyMethodList(clz, &methodCount);

    printf("Found %d methods on '%s'\n", methodCount, class_getName(clz));

    for (unsigned int i = 0; i < methodCount; i++) {
        Method method = methods[i];

        printf("\t'%s' has method named '%s' of encoding '%s'\n",
               class_getName(clz),
               sel_getName(method_getName(method)),
               method_getTypeEncoding(method));

        /**
         *  Or do whatever you need here...
         */
    }

    free(methods);
}

You will need to make two separate calls to this method. One for the instance methods and another for the class methods:

/**
 *  This will dump all the instance methods
 */
DumpObjcMethods(yourClass);

Calling the same on the metaclass will give you all the class methods

/**
 *  Calling the same on the metaclass gives you
 *  the class methods
 */
DumpObjcMethods(object_getClass(yourClass) /* Metaclass */);
3
  • 2
    The import path is hard to find, use: #import <objc/runtime.h>
    – prewett
    Jun 3, 2015 at 22:36
  • Thanks, may be a typo, "DumpObjcMethods(object_getClass(yourClass) /* Metaclass /);" should be: "DumpObjcMethods(object_getMetaClass(yourClass) / Metaclass */);"
    – Xueshi
    Aug 7, 2016 at 15:51
  • @Joey: I don't think there is a typo there. There is no routine called object_getMetaClass. The class objects class is its meta class.
    – Buzzy
    Aug 8, 2016 at 17:39
9

You'll want to use the Objective C runtime methods, see here: https://developer.apple.com/reference/objectivec/objective_c_runtime

2
  • 1
    This link now redirects to navigation page Feb 14, 2017 at 12:42
  • 1
    Apples docs have gotten more horrible. Buzzy's answer below is a much better answer.
    – Paul
    Aug 7, 2017 at 15:27
4

This is possible via objc_method_list. In order to enumerate your methods, you will have to register all your methods before hand.

The process is straight forward: after you've declared your function you can create an instance of objc_method and register the function name. Then add the objc_method to a objc_method_list and finally pass the objc_method_list to class_addMethods..

Here is a link to get you started: http://theocacao.com/document.page/327

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.