APT 17: List of all Objective-C Methods in a Class

Some time ago I was trying to remember a method name that I wanted to call in order to achieve the desired effect. Usually the Xcode app is able to list all the possible methods, but I had some errors in the file, so Xcode was confused and I could not fix all of it at the same time. So I decided, that I should implement a method, that lists all possible methods of a given class.

This is relatively easy in Objective-C, since the method names are inside every program, because the name resolution is done at runtime in Objective-C. All I had to do was implement some code fragment like this:

 #import <objc/runtime.h>
 ... 
 Class elem = [objectToBeListed class]; 
 while (elem) {
     NSLog(@"%s", class_getName( elem ));
     unsigned int numMethods = 0;
     Method *mList = class_copyMethodList(elem, &numMethods);
     if (mList) {
         for (int j=0; j < numMethods; j++) {
             NSLog(@" %s", 
                 sel_getName(method_getName(mList[j])));
         }
         free(mList);
     }
     if (elem == [NSObject class]) {
         break;
     }
     elem = class_getSuperclass(elem);
 }

This gives a fairly long listing of all methods of your class and all its super classes.

And it also revealed some humor from the Apple guys, when I listed a UIViewController Class. Because in the listing there I found the following class name (broken in to smaller pieces for better readability):

attentionClassDumpUser:yesItsUsAgain:
althoughSwizzlingAndOverridingPrivateMethodsIsFun:
itWasntMuchFunWhenYourAppStoppedWorking:
pleaseRefrainFromDoingSoInTheFutureOkayThanksBye:

If you find that hard to read, here is the readable version of that “sentence”:
attention Class Dump User: yes Its Us Again: although Swizzling And Overriding Private Methods Is Fun: it Wasnt Much Fun When Your App Stopped Working: please Refrain From Doing So In The Future Okay Thanks Bye:

To swizzle is computer-speak for changing something, for “messing around”. Although I had not planned to swizzle with any pointer or to override any method, I did feel like this sentence was directed to people like me, dumping the list of methods.

Of course, the documentation does not list this function. And there is no such function in Mac OS X, so this is an iPhone “feature”.