# Reflection in Objective-C



## KenDRhyD (Oct 1, 2006)

There are numerous oblique references to the abitlity to examine Objective-C instances and classes at runtime to determine specifics about their nature (such as their name and a list of the methods/selectors defined), but I can find no specific details on this. All of the examples use "isa" or "respondsToSelector", which require that my code know what I am looking for first.

What I want is, given the name of a class or an object instance of that class, to be able to print out the name of the class (actually found that in the "description" method/selector), a list of the data members, complete with their name and type, and a list if the defined selectors, along with their name, type, parameters, and scope (private, etc.). If all I can examine is public information, OK for now, but I can find no specific details on how to do this at all.

Help!


----------



## Mikuro (Oct 1, 2006)

Here's a little code snippet I wrote a while back while trying to deconstruct some private WebKit classes.

```
{
	int *x[1] = {0};
	struct objc_method_list* ml;
	while ( (ml = class_nextMethodList(NSClassFromString(@"NSView"), (void**)&x)) ) {
		int i;
		for (i=0; i<(*ml).method_count; i++) {
			NSLog(@" %s",(*ml).method_list[i].method_name);
		}
	}
}
```

This will dump the name of every selector defined in that class (not its superclasses, though).

There a bunch of low-level objective-C functions detailed in Apple's ObjCRuntimeRef page (also available in XCode's built-in documentation). I haven't really mastered this myself, so that's about all I can tell you.

The author of SIMBL and PithHelmet has an interesting page on Cocoa Reverse Engineering that you might want to read as well.


----------

