我想知道在某个子类中是否有可能“识别”在其超类中声明的方法作为在协议中声明的方法的实现子类所遵循的,因为它们具有相同的签名。通过在超类中声明的方法实现协议?
它甚至很难制定这个,这就是为什么我所做的任何搜索结果迄今为止没有结果。
但让我来举一个例子来更好地理解。
协议:
@protocol TheProtocol <NSObject>
- (void)theProtocolMethod;
@end
超类:
// Superclass does not adhere to TheProtocol
@interface TheSuperClass : NSObject
- (void)theProtocolMethod;
@end
@implementation TheSuperClass
- (void)theProtocolMethod
{
// stuff
}
@end
子类:
// SubClass adheres to TheProtocol but does not implement it's methods
// since they are implemented in the class it is subclassing. Is this OK?
@interface TheSubClass : TheSuperClass <TheProtocol>
@end
@implementation TheSubClass
@end
这是任何接近被 “OK”?
TheSubClass
实际上是TheSuperClass
的一个类别(声明遵守TheProtocol
)而不是子类?
多一点背景:
这是涉及到核心数据的情况。我需要发布一个在实体类中定义的访问器,这个访问器将在一个单独的框架中用于为我的应用程序开发插件。协议本身完全由核心数据实体类的类别实现,除了在实体类本身实现的所述访问器之外,因此我的问题。
你试过看它是否有效吗?如果它不起作用,它应该抱怨你没有从协议中实现一个方法,但是如果它有效,那么应该没有警告。 – Gavin
我最终尝试了类别选择。确实没有警告出现。从基类中除去方法声明点亮了关于未实现方法的警告。所以我想它在他结束的时候会有诀窍。 – insys