In the interface I have this:
Animal* myPet;
At runtime I may want myPet to be a cat or a dog, which are subclasses of Animal:
id newPet;
if(someCondition) {
newPet = [[Cat alloc] initWithNibName:@"Cat" bundle:nil];
} else {
newPet = [[Dog alloc] initWithNibName:@"Dog" bundle:nil];
}
self.myPet = newPet;
Obviously this is incorrect, but I hope it's enough to show what I'm trying to do. What is the best practice for doing this?
isKindOfClass
is your friend:
[newPet isKindOfClass:Dog.class] == NO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With