[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: isKindOf: - GSObjCIsKindOf
From: |
Marko Mikulicic |
Subject: |
Re: isKindOf: - GSObjCIsKindOf |
Date: |
Sat, 25 Aug 2001 11:06:49 -0400 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.3) Gecko/20010801 |
Pascal Bourguignon wrote:
Marko Mikulicic <mikulici@die.supsi.ch> wrote:
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);
}
As you can see, there is a class method named isKindOfClass:. You can
send this message to a class. On another hand, all classes are always
instanciated, you don't loose anything invoking them, meaning that if
for syntactic reason you need to write [MyClass class] instead of
MyClass, there's no downside (AFAIK).
[MyFirstClass isKindOfClass:MySecondClass]
That doesn't work!
assert(GSObjCIsKindOf(NSClassFromString(@"ICalendarDate")
,
NSClassFromString(@"NSCalendarDate")));
assert([NSClassFromString(@"ICalendarDate")
isKindOfClass:
NSClassFromString(@"NSCalendarDate")]);
fails the second assertions.
It seems that the class method +isKindOfClass: has precedence over the
instance method -isKindOfClass: defined in NSObject (an thus inherited
by every class object).
The +isKindOfClass: is defined with the == operator and NSObject.
Is this correct ? Shouldn't it return GSObjCIsKindOf(self,aClass) ?
(What should be the result of GSObjCClass(aClass) ?
I have heard something about fixing that in gcc 3.0.1...)
Metaclasses where are you ? :-)
If you only have the class name at run time stored in a NSString, you
can use the function NSClassFromString (found in NSObjCRuntime.h) to
get the class with the given name.
Class Class1=NSClassFromString(@"MyFirstClass");
Class Class2=NSClassFromString(@"MySecondClass");
if((Class1!=nil)&&(Class2!=nil)){
return([Class1 isKindOfClass:Class2]);
}else{
return(NO);
}
Thanks, I was using NSBundle, but probabliy this is much faster.
Is NSBundle mainBundle classNamed: just slower or it has
a different class searching semantics ?
(Currently I have this objects linked in)
Marko
- isKindOf: - GSObjCIsKindOf, Marko Mikulicic, 2001/08/24
- Re: isKindOf: - GSObjCIsKindOf, Pascal Bourguignon, 2001/08/24
- Re: isKindOf: - GSObjCIsKindOf,
Marko Mikulicic <=
- Re: isKindOf: - GSObjCIsKindOf, Nicola Pero, 2001/08/28
- Re: isKindOf: - GSObjCIsKindOf, Marko Mikulicic, 2001/08/29
- Re: isKindOf: - GSObjCIsKindOf, Stefan Böhringer, 2001/08/30
- Re: isKindOf: - GSObjCIsKindOf, Marko Mikulicic, 2001/08/30
- Message not available
- Re: isKindOf: - GSObjCIsKindOf, Stefan Böhringer, 2001/08/31
- Re: isKindOf: - GSObjCIsKindOf, Nicola Pero, 2001/08/30
Re: isKindOf: - GSObjCIsKindOf, Nicola Pero, 2001/08/28