discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: isKindOf: - GSObjCIsKindOf


From: Nicola Pero
Subject: Re: isKindOf: - GSObjCIsKindOf
Date: Tue, 28 Aug 2001 13:07:30 +0100 (BST)

> Hello,
> 
> Is there a way, knowing the name of two classes, test for
> the isKindOf relation without istantiating the receiver object:
> 
> I have noticed that those methods in NSObject are defined so:
> 
> 
> - (BOOL) isKindOf: (Class)aClassObject
> {
>    return [self isKindOfClass: aClassObject];
> }
> 
> + (BOOL) isKindOfClass: (Class)aClass
> {
>    if (aClass == [NSObject class])
>      return YES;
>    return NO;
> }
> 
> - (BOOL) isKindOfClass: (Class)aClass
> {
>    Class class = GSObjCClass(self);
> 
>    return GSObjCIsKindOf(class, aClass);
> }
> 
> 
> Is there a way other than using GSObjCIsKindOf
> directly to look if one class inherits from the other one ?

What about something like

#include <Foundation/Foundation.h>

@interface NSObject (ClassInheritance)
+ (BOOL) isSubclassOfClass: (Class)aPotentialSuperClass;
@end
   
@implementation NSObject (ClassInheritance)
+ (BOOL) isSubclassOfClass: (Class)aPotentialSuperClass
{
  if (self == aPotentialSuperClass)
    {
      return YES;
    }
  else
    {
      Class c = self;
      
      while (c != Nil)
        {
          c = [c superclass];
          
          if (c == aPotentialSuperClass)
            {
              return YES;
            }
        } 
      return NO;
    }
}
@end

and then of course in your code, you can test using

[NSString isSubclassOfClass: [NSObject class]]; // YES
[NSObject isSubclassOfClass: [NSString class]]; // NO

if you have the class name as a string you can always use
NSClassFromString () to get the Class pointer from the class name.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]