[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Checking the super class of a class
From: |
Sašo Kiselkov |
Subject: |
Re: Checking the super class of a class |
Date: |
Thu, 06 Oct 2005 22:34:57 +0200 |
User-agent: |
Internet Messaging Program (IMP) 3.2.5 |
Quoting Richard Frith-Macdonald <richard@brainstorm.co.uk>:
> On 2005-10-06 15:49:15 +0000 Andreas Höschler <ahoesch@smartsoft.de> wrote:
>
> > Hi all,
> >
> > I need something like [something isKindOfClass:...] with something being
> not
> > an instance but a class created with NSClassFromString(). A probably
> working
> > but very ugly hack would be to create an instance of this class with
> > [[something alloc] init] and releasing the object after the check. But I am
> > sure there is a better approach. Any idea?
>
> BOOL isDerivedFrom(Class this, Class base)
> {
> if (this == base) return YES;
> this = [this super];
> if (this == 0) return NO;
> return isDerivedFrom(this, base);
> }
I like avoiding recursion :-) so:
BOOL IsDerivedFrom(Class this, Class base)
{
Class myClass;
for (myClass = this; myClass != Nil; myClass = [myClass superclass])
{
if (myClass == base)
return YES;
}
return NO;
}
though the actual speed benefit is near zero :-P
--
Saso