Re: Finding all subclasses of a class ?
Re: Finding all subclasses of a class ?
- Subject: Re: Finding all subclasses of a class ?
- From: glenn andreas <email@hidden>
- Date: Thu, 25 Aug 2005 11:25:07 -0500
On Aug 25, 2005, at 10:38 AM, Eric Morand wrote:
Le 25 août 05 à 16:53, glenn andreas a écrit :
Make sure to include <objc/objc-class.h>
Of course ! Shame on me !!!
Anyway, it doesn't work : only two of all my subclasses are found.
And I'm not using Zerolink.
I'd start by printing out all the classes it find, to make sure that
it's finding the classes correctly (regardless if it thinks the
subclasses are correct). And yes, for the record, if you use
Zerolink, code like this doesn't work because until you use a class,
it's object file won't be linked in, so you won't find it.
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:
This email sent to email@hidden