Re: Finding all subclasses of a class ?
Re: Finding all subclasses of a class ?
- Subject: Re: Finding all subclasses of a class ?
- From: Daniel Jalkut <email@hidden>
- Date: Thu, 25 Aug 2005 14:33:58 -0400
Thanks for sharing this - it makes the basis for a nice command-line
tool for exploring arbitrary AppKit class lineage...
Daniel
On Aug 25, 2005, at 12:25 PM, glenn andreas wrote:
Here's some code that works in an application of mine (and since
it's from a working app, it doesn't suffer from "pre coffee typos"):
NSMutableSet *gAllFoos = [NSMutableSet set];
int numClasses = 0, newNumClasses = objc_getClassList(NULL, 0);
Class *classes = NULL;
while (numClasses < newNumClasses) {
numClasses = newNumClasses;
classes = (Class *)realloc(classes, sizeof(Class) *
numClasses);
newNumClasses = objc_getClassList(classes, numClasses);
}
// now, can use the classes list; if NULL, there are no classes
for (int i=0;i<numClasses;i++) {
if (classes[i] == [Foo class])
continue; // don't add our abstract class
if (!CLS_GETINFO(classes[i], CLS_CLASS))
continue; // ignore meta classes as well
Class s = classes[i];
while (s) { // walk the inheritence because we don't know about
this class at all
if (s == [Foo class])
break;
s = s->super_class;
}
if (s) {
// do something with it...
[gAllFoos addObject: classes[i]];
}
free(classes);
return gAllFoos;
Note that this will also find the various notifying classes
(magically created by KVO), so you'll need some way to filter those
out (and the way I'm doing it is based on some hierarchy specific
behavior, so it's been redacted).
Glenn Andreas email@hidden
<http://www.gandreas.com/> wicked fun!
Widgetarium | the quickest path to widgets
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
sweater.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden