I have the following design :
@protocol SomeProtocol <NSObject>
@optional
- (void)someMethod;
@end
@interface SomeObject : NSObject <SomeProtocol>
@end
I want to provide a default implement of someMethod
for my protocol so I make a category like so :
@interface SomeObject (SomeCategory) <SomeProtocol>
- (void)someMethod;
@end
@implementation SomeObject (SomeCategory)
- (void)someMethod
{
// default implementation for all classes implementing the protocol SomeProtocol
}
@end
Now I also want to leave the possibility to implement a custom implementation of this method. If I implement directly on the SomeObject
class like so :
@interface SomeObject <SomeProtocol>
- (void)someMethod;
@end
@implentation SomeObject
- (void)someMethod
{
// custom implementation for SomeObject class
}
@end
This implementation seems to be prioritized over the category method. But Apple documentation says :
If the name of a method declared in a category is the same as a method in the original class, or a method in another category on the same class (or even a superclass), the behavior is undefined as to which method implementation is used at runtime.
I did not think yet about other possibilities which could be better (like working with a base class and subclasses maybe) but is what I'm doing here a good design or will it necessary lead to undefined behavior ?
Thanks in advance for your help