Hello everyone. I've been lurking here for awhile now, and I want to contribute. So I'm starting a thing here, posting Cocoa tips about once per week. The first one I want to post is this:
Sometimes you will be 'reverse engineering' a framework or bundle, or will want to know more about certain AppKit or Foundation classes. Class-dump works in a great many situations, but when it doesn't, this code fragment will often do the trick:
Code:
void *iterator = 0;
struct objc_method_list *mlist;
int offset, i;
i = offset = 0;
mlist = class_nextMethodList( ourObjcClass, &iterator );
NSLog(@"the class %@ has the following methods:", ourObjcClass);
while( mlist != NULL )
{
while( i < mlist->method_count) {
NSLog(@"%@ has %i arguments, which are %i in size", NSStringFromSelector(mlist->method_list[i].method_name),method_getNumberOfArguments(&mlist->method_list[i]) , method_getSizeOfArguments(&mlist->method_list[i]));
i++;
}
i = 0;
mlist = class_nextMethodList( ourObjcClass, &iterator );
}
A couple of things are interesting to note here. The number of arguments will be 2 more than what you'd expect, because objc methods have 2 hidden arguments: the pointer to the object, and the name of the selector. Another thing to note is that you MUST include "objc-class.h" in your project and at the top of whatever class you're doing this in, or you will get errors. Check out file://Developer/Documentation/Cocoa/ObjectiveCLanguage-title.html for more information on the ObjC language's features.