Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
Example: I have a class named Animal. There are multiple subclasses of Animal, one of which is named Pet. There are four subclasses of Pet, named PetDog, PetCat, PetFish, and PetBird.

I want my app to check whether an object is an instance of a subclass of Animal. I'm thinking of doing something like this:

Code:
bool objectHasClassAsAncestor = false;
Class class = [object class];
while (([class superclass] != [Animal class]) & ([[object class] superclass] != [NSObject class])
{
class = [class superclass];
}
if ([class superclass] == [Animal class])
{
objectHasClassAsAncestor = true;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.