so I have subclassed a UIViewController that I create. The question is, is there a way so that I can force to call a method using the subclassed one if the one I am creating is the super class. I know this is stupid, as it’s the same as forcing a mammal to call a dog’s behavior. But before doing this, I am actually making sure first that that mammal is a dog, otherwise I won’t call it. Is this a bad thing to do? If not then how can I do such thing? I tried casting the superclass into it’s subclass and then do a method call, but it doesn’t work. Any thoughts?
So what I am trying to do. I have a UIViewController A and a subclass of that B. If I have A and I want to call B’s method that overrides A, how do I do that? I hope this is clear
There is a difference between how you declare your object and what it actually is.
Consider this example:
Here, you have a pointer to a
Mammalobject which actually points to an instance of the specializedDogclass. This is valid because a mammal has all the methods that a dog has.You can check if some
Mammalinstance is actually aDogby usingisKindOfClass:, like this:This is perfectly valid. If however you would have created your
mammalwith[[Mammal alloc] init], the check for theDogclass would fail and you would not be able to call any methods that onlyDoghas. If you wouldn’t do the check, this would actually crash, because the casting does not change the object in any way, it just tells the compiler “I know what I’m doing, don’t warn me”.