I have a base class which adds some functionality to a number of derived classes in my app.
One of these features is only used by some subclasses.
Currently I’m using a method which returns a BOOL which defaults to NO to “short-circuit” this feature. Subclasses which want the feature must override this method and return YES.
This feature is only useful if you’ve also overridden at least one of two other methods.
I’d prefer to use class_copyMethodList to determine if the subclass implemented either of these two methods (instead of using the method which returns a BOOL).
What barriers/roadblocks/cons to this approach should I be aware of? Is there a standard implementation of this idiom which I can use?
If I may suggest an alternative approach, have you considered using
-instanceMethodForSelectoron the relevant subclass instance and comparing to the result on the base class?That method returns an
IMP, which is a C function pointer to the implementation for the given selector. So if the subclass has a different implementation from the base class, it’ll return a differentIMP.EDIT: as discussed in the comments below, a further alternative is to declare a formal protocol that the sub classes may implement, and to use
NSObject‘s-conformsToProtocol:to determine whether the protocol is implemented. SinceconformsToProtocolreturns whether the class has declared support for the protocol (in its@interfacevia the angle brackets syntax), that’s a lot like adding a customBOOLmethod that defaults to returningNObut without the syntactic and semantic overhead of adopting your own ad hoc solution.