• 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: Finding all subclasses of a class ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Finding all subclasses of a class ?


  • Subject: Re: Finding all subclasses of a class ?
  • From: "John C. Randolph" <email@hidden>
  • Date: Thu, 25 Aug 2005 14:56:43 -0700


On Aug 25, 2005, at 6:59 AM, Eric Morand wrote:

Is there a way to find all subclasses of a class ?

I've done it like this, in the past:

#define ROOT_CLASSES_KEY @"ROOT_CLASSES_KEY"

@implementation RuntimeReporter

NSMutableDictionary *classLookupCache;
NSMutableData *rawClassPointers;

+ (int) numberOfClasses {  return objc_getClassList(nil, 0); }

+ (NSData *) rawClassPointers
{
int numClasses = [self numberOfClasses];
rawClassPointers = [[NSMutableData alloc] initWithLength: numClasses * sizeof(Class)];
void *bytes = [rawClassPointers mutableBytes];
int classesRetrieved = objc_getClassList(bytes, numClasses);
return [rawClassPointers autorelease];
}


// This method always checks the runtime, not the cached results! Don't use this directly, use -subclassNamesForClassNamed: if you care about efficiency.
+ (NSArray *) subclassNamesForClass:(Class) aClass // Pass Nil for root classes.
{
id
names = [NSMutableArray array];


    Class
        *classPointers = (Class *) [[self rawClassPointers] bytes];

    int
        index = [self numberOfClasses];

while (index --)
{
Class thisClass = classPointers[index];
if (thisClass->super_class == aClass)
[names addObject:NSStringFromClass(thisClass)];
}
names = [names sortedArrayUsingSelector:@selector(compare:)];
[[self lookupCache] setValue:names forKey:(aClass ? NSStringFromClass(aClass) : ROOT_CLASSES_KEY)];
return names;
}


+ (NSArray *) subclassNamesForClassNamed:(NSString *) className // Pass nil for the root classes.
{
NSArray *result = [classLookupCache valueForKey:( className ? className : ROOT_CLASSES_KEY)];
return result ? result : [self subclassNamesForClass:NSClassFromString(className)];
}


@end

One caveat: In Tiger, KVO can add classes to the runtime anytime you add an observer for a keypath. This defeats the caching I was doing with the NSMutableDictionary "classLookupCache".

-jcr


John C. Randolph <email@hidden> (408) 914-0013 Roaming Cocoa Engineer, Available for your projects at great Expense and Inconvenience.



_______________________________________________
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


References: 
 >Finding all subclasses of a class ? (From: Eric Morand <email@hidden>)

  • Prev by Date: Re: [Moderator] Re-posts (Re: How to draw correct underline ?)
  • Next by Date: Re: How to draw correct underline ?
  • Previous by thread: Re: Finding all subclasses of a class ?
  • Next by thread: QTMovieView in a split view - very weird stuff
  • Index(es):
    • Date
    • Thread