Is it possible to check whether a subclass implements a method that exists either in its immediate superclass or in some superclass of its superclass, etc?
E.g. I subclass UIView
, to make my custom generic view for my app, and then I subclass this custom view. Now some of my subclasses override some method from UIView
and some don't. I only want to call this method if it is actually overridden, I do not want the default UIView
method being called.
Is there a way to check this, i.e. with a method similar to respondsToSelector:
?
Edit: This question is different from the one asked in Objective-C detect if class overrides inherited method because I do not really know or do not want to care which superclass originally implements the method.
getMethod("myMethod"). getDeclaringClass(); If the class that's returned is your own, then it's not overridden; if it's something else, that subclass has overridden it.
Note: In a subclass, you can overload the methods inherited from the superclass. Such overloaded methods neither hide nor override the superclass instance methods—they are new methods, unique to the subclass.
A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final. A subclass in a different package can only override the non-final methods declared public or protected.
When a method in a subclass has the same name, same parameters or signature, and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class. Method overriding is one of the way by which java achieve Run Time Polymorphism.
Based on this answer by Mert Buran.
You can create a simple method to check whether a given object overrides a given selector (method):
-(BOOL)checkIfObject:(id)object overridesSelector:(SEL)selector {
Class objSuperClass = [object superclass];
BOOL isMethodOverridden = NO;
while (objSuperClass != Nil) {
isMethodOverridden = [object methodForSelector: selector] !=
[objSuperClass instanceMethodForSelector: selector];
if (isMethodOverridden) {
break;
}
objSuperClass = [objSuperClass superclass];
}
return isMethodOverridden;
}
This can be called as follows:
[self checkIfObject:someObject overridesSelector:@selector(someSelector)];
Unfortunately, there is no equivalent to respondsToSelector:
that will do precisely this job for you. As you will probably know, respondsToSelector:
works in the way that it will return YES
as long as the class itself or any of its superclasses implements this method.
But why not just put an empty implementation of the method into your custom subclass, that way you make sure that calling it doesn't have any effect and doesn't call the same method in the superclass. Did you think about this?
Update:
While there is no method equivalent to respondsToSelector:
, you might want to take a look at the Objective-C Runtime. It's a library that allows you to inspect characteristics of a class during runtime (a bit similar to Java reflections). Check out the reference here.
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