Skip navigation.
 
mlRe: Iterating over a classes methods?
FROM : John C. Randolph
DATE : Fri Dec 27 23:35:49 2002

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_removed>>  (408) 974-8819
Sr. Cocoa Software Engineer,
Apple Worldwide Developer Relations
http://developer.apple.com/cocoa/index.html
_______________________________________________
cocoa-dev mailing list | <email_removed>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

Related mailsAuthorDate
mlIterating over a classes methods? Victor Ng Dec 24, 08:47
mlRe: Iterating over a classes methods? Timothy Ritchey Dec 24, 15:33
mlRe: Iterating over a classes methods? gparker-apple Dec 24, 20:10
mlRe: Iterating over a classes methods? John C. Randolph Dec 27, 23:35