0

I am loping an array of object that are of a type. More specific types that inherit this type. How do I check if an object is of a certain more specific type?

In the example below I am trying to check if an animal is also an elephant.

        for (NSInteger i = 0; i < allAnimals.count; i++) {
            Animal *animal = allAnimals[i];

            // Check if animal is Elefant?                
        }
user1283776
  • 19,640
  • 49
  • 136
  • 276

1 Answers1

1

You can use isKindOfClass method for it

for (NSInteger i = 0; i < allAnimals.count; i++) {
    Animal *animal = allAnimals[i];

    if([animal isKindOfClass:[Elephant class]])
        NSLog(@"Yes it is an Elephant");
}

I have assumed Elephant as a class here

Arun
  • 1,391
  • 1
  • 10
  • 29