我有以下设计:
@protocol SomeProtocol <NSObject>
@optional
- (void)someMethod;
@end
@interface SomeObject : NSObject <SomeProtocol>
@end
我想为我的协议(protocol)提供 someMethod
的默认实现,所以我创建了一个这样的类别:
@interface SomeObject (SomeCategory) <SomeProtocol>
- (void)someMethod;
@end
@implementation SomeObject (SomeCategory)
- (void)someMethod
{
// default implementation for all classes implementing the protocol SomeProtocol
}
@end
现在我还想保留实现此方法的自定义实现的可能性。如果我像这样直接在 SomeObject
类上实现:
@interface SomeObject <SomeProtocol>
- (void)someMethod;
@end
@implentation SomeObject
- (void)someMethod
{
// custom implementation for SomeObject class
}
@end
此实现似乎优先于类别方法。但是 Apple 文档说:
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.
我还没有考虑其他可能更好的可能性(比如可能使用基类和子类),但我在这里做的是一个好的设计还是会导致未定义的行为?
预先感谢您的帮助
最佳答案
关于这个问题,文档不是很清楚。除了到目前为止提到的内容之外,在文档的其他一些地方,它说:“当类别覆盖继承的方法时,类别中的方法可以像往常一样通过消息调用继承的实现。但是,如果类别重写类别类中已经存在的方法,则无法调用原始实现”——我将其解读为“类别实现将始终运行,而不是原始实现”。
我以前读过这个但现在没找到,但是在this SO answer中提到了它.
就个人而言,我经常使用类别覆盖方法,到目前为止,它没有给我带来任何麻烦。但是,因为我知道我会“杀死”原始实现,所以我谨慎地只使用可以安全地假设该类从父类(super class)继承原始实现的方法。
例如,我认为在 UIView
的类别中覆盖 touchesBegan:withEvent:
是安全的,因为该方法是从 UIResponder< 继承的
并且原始实现保持不变,可以(并且应该)通过从类别的实现中调用 super
来实现。
据我所知,抽象或空的实现也可以安全地覆盖。例如,我一直在 UIScrollView
中重写 awakeFromNib
以解决 ScrollView 未根据实际内容大小自行设置其 contentSize 属性的问题,以防万一已在 Interface Builder 中创建:
@implementation UIScrollView (MyHandyCategory)
-(void)awakeFromNib {
[self autoResizeContent];
}
-(void)autoResizeContent {
NSArray *subViews = [self subviews];
UIView *contentView = [subViews objectAtIndex:0];
[self setContentSize:contentView.frame.size];
}
@end
如果您想知道为什么我不只是将自动调整大小代码放在 awakeFromNib 中 - 我希望也能够在其他场合调用自动调整大小。在我的实际类别中,awakeFromNib 中有更多代码,我不一定希望与自动调整大小代码一起运行。
关于ios - 直接在类上实现时类别方法无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21515284/