How can I find out if a superclass also implements the same method that my subclass implements?

I want to know if a superclass implements class A from my subclass, which also implements method A, so I can safely call [super A] from my subclass without getting an exception.

NSObject responds with SoSelector: in this case it does not work, since it will always return true (because my subclass implements this method). Any ideas?

+5
source share
3 answers

To do this, you can use the class method instancesRespondToSelector:. Therefore, from a subclass, you can call [[self superclass] instancesRespondToSelector:@selector(...)]to determine if the superclass implements the class that you need.

+12
source

[[self superclass] instancesRespondToSelector:@selector(...)] , , , . , :

[[ExplicitClassName superclass] instancesRespondToSelector:@selector(...)]
+2

Try [[[self class] superclass] instances ofRespondToSelector: @selector (.)]]]

[super responds to SoSelector:] calls the implementation of the responseToSelector superclass, which most likely matches the current class. It does not check if a superclass implements a class.

0
source

All Articles