Re: Iterating over a classes methods?
Re: Iterating over a classes methods?
- Subject: Re: Iterating over a classes methods?
- From: "John C. Randolph" <email@hidden>
- Date: Fri, 27 Dec 2002 14:35:49 -0800
On Monday, December 23, 2002, at 11:47 PM, Victor Ng wrote:
Is there a way to iterate over the list of messages that an object
will respond to?
I did it like this: (slightly modified from an example that Erik Buck
posted here some time ago.)
NSArray *
methodNamesForClass(Class aClass)
{
Class
class = aClass;
NSMutableArray
*methodNames = [NSMutableArray array];
// *methodNames = (class->super_class ?
methodNamesForClass(class->super_class) : [NSMutableArray array]);
struct objc_method_list
*methods;
void
*iterator = NULL;
while(methods = class_nextMethodList(aClass, &iterator ))
{
int i = methods->method_count;
while(i--)
{
Method currentMethod = &methods->method_list[i];
[methodNames addObject:
NSStringFromSelector(currentMethod->method_name)];
}
}
return [NSArray arrayWithArray:methodNames];
}
The method above will show you only the methods implemented in aClass,
not its inherited methods. To get all of the method names, comment out
the line "*methodNames = [NSMutableArray array];", and uncomment the
line after it.
-jcr
John C. Randolph <email@hidden> (408) 974-8819
Sr. Cocoa Software Engineer,
Apple Worldwide Developer Relations
http://developer.apple.com/cocoa/index.html
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.