• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: class method list
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: class method list


  • Subject: Re: class method list
  • From: "John C. Randolph" <email@hidden>
  • Date: Mon, 4 Nov 2002 15:35:23 -0800

On Friday, November 1, 2002, at 03:36 PM, Eric Cole wrote:

Given an arbitrary object (Object *it), how is the class object of that object retrieved. Say that the object was an NSView, how would you get [NSView class] from the object?

NSView *myView;

Class myViewClass = [myView class];

or

id someObject;
Class itsClass = [someObject class];

can you say [[it class] class];?

Ultimately, the goal is to get a list of class methods for an arbitrary object. Getting the instance methods is well documented, but I saw no mention of class methods beyond class_getClassMethod. How is the list of selectors obtained?

I did it like this (somewhat modified from an example that Eric Buck posted in comp.lang.objective-c a few days back):

NSArray *
methodNamesForClass(Class aClass)
{
Class
class = aClass;

NSMutableArray
*methodNames = [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];
}

This is a function of course, because not all objects descend from NSObject.

So, to obtain the names of all the methods to which a given object can respond, you'd do something like:

id anObject;

NSArray *allMethodsAnObjectRespondsTo = methodNamesForClass([anObject class]);


-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.

References: 
 >class method list (From: Eric Cole <email@hidden>)

  • Prev by Date: Re: Clearing cells in NSBrowser
  • Next by Date: Re: [Cocoa-Java] Aquataxx source code
  • Previous by thread: Re: class method list
  • Next by thread: Bi-directional Text
  • Index(es):
    • Date
    • Thread