• 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: Eric Morand <email@hidden>
  • Date: Thu, 25 Aug 2005 16:37:03 +0200

Thanks Glenn.


I'm having a compilatin error with this line :

cls = cls->super_class;


"Dereferencing pointer to incomplete type"



In fact, I can call anything on cls (cls->anything for instance), I have the same error.



You should also never NSLog a "raw" variable/expression (i.e., without using a format string) because if it happened to contain a format character, you'll probably crash.

I did not know this ! Thanks for the tip.




Eric.


Le 25 août 05 à 16:16, glenn andreas a écrit :


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


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

At runtime, I want to be able to find all subclasses of my custim Class : EutopiaModule.



Here is what I'm doing :

    int numClasses;
    Class * classes = NULL;
    classes = NULL;
    numClasses = objc_getClassList(NULL, 0);

    if( numClasses > 0 )
    {
        classes = malloc( sizeof(Class) * numClasses );
        (void) objc_getClassList( classes, numClasses );
    }

    int i;

for (i = 0; i < numClasses; i++ )
{
if ( [classes[i] respondsToSelector:@selector (isSubclassOfClass:)] )
{
if ( [classes[i] isSubclassOfClass:[EutopiaModule class]] )
{
NSLog (NSStringFromClass(classes[i]));
}
}
}



It doesn't work. The app crash with the following messages :

2005-08-25 15:57:10.104 €utopia[11296] *** +[Object respondsToSelector:]: warning: Object compatibility method has been executed at least once. Convert source code off it NOW!
2005-08-25 15:57:10.108 €utopia[11296] AccountsTransactionsModule
2005-08-25 15:57:10.116 €utopia[11296] *** Selector 'respondsToSelector:' sent to dealloced instance 0xa2881c9c of class UNKNOWN.
Break at '-[_NSZombie respondsToSelector:]' to debug.
2005-08-25 15:57:10.116 €utopia[11296] An uncaught exception was raised
2005-08-25 15:57:10.116 €utopia[11296] *** Selector 'respondsToSelector:' sent to dealloced instance 0xa2881c9c of class UNKNOWN.
Break at '-[_NSZombie respondsToSelector:]' to debug.
2005-08-25 15:57:10.116 €utopia[11296] *** Uncaught exception: <NSGenericException> *** Selector 'respondsToSelector:' sent to dealloced instance 0xa2881c9c of class UNKNOWN.
Break at '-[_NSZombie respondsToSelector:]' to debug.





The reason it is breaking is that you are running into a few legacy support classes (namely, Object and any subclasses of Object). You then use "respondsToSelector" which isn't directly implemented for Object (which is why you get the "Object compatibility method" warning). There are other problems with a few other classes that exist that aren't subclasses of NSObject (or don't implement the NSObject protocol).





What is the right way to do this ?



Walk that superclass chain instead of using "isSubclassOfClass" because that uses the runtime which is completely independent of class hierarchy and any class specific methods.


See objc/objc-class.h for details, but something like this (typed in mail and before I've finished morning coffee) :

struct objc_class *cls = (struct objc_class *)classes[i];
while (cls != NULL) {
    if (cls == (struct objc_class *)[EutopiaModule class]) {
        NSLog (@"%@",NSStringFromClass(classes[i]));
        break;
    }
    cls = cls->super_class;
}

You should also never NSLog a "raw" variable/expression (i.e., without using a format string) because if it happened to contain a format character, you'll probably crash.


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
  • Follow-Ups:
    • Re: Finding all subclasses of a class ?
      • From: glenn andreas <email@hidden>
References: 
 >Finding all subclasses of a class ? (From: Eric Morand <email@hidden>)
 >Re: Finding all subclasses of a class ? (From: glenn andreas <email@hidden>)

  • Prev by Date: Re: AESend() error - unknown symbol '_AESend'
  • Next by Date: Re: Converting JPEG Image Greater Than 72dpi To 72dpi
  • Previous by thread: Re: Finding all subclasses of a class ?
  • Next by thread: Re: Finding all subclasses of a class ?
  • Index(es):
    • Date
    • Thread