如何重写超类?有更具体类型的财产吗?
场景
我遇到一种情况,名为 AbstractRequest
的基类具有在头文件中声明的 id
类型的委托属性:
@property (nonatomic, assign) id <AbstractRequestDelegate> delegate;
抽象委托协议包含一些必需的方法,并且如“abstract”一词所示,AbstractRequest
和 AbstractRequestDelegate
都旨在成为子类/扩展。
其中一个例子是子类 ConcreteRequest 和扩展协议 ConcreteRequestDelegates,它们都为抽象方法添加了额外的方法。目的是抽象类方法和具体类方法都可以向单个分配的委托实例发送消息。
在某个时间点,ConcreteRequest 希望调用 ConcreteRequestDelegate 定义的委托上的方法。因为委托的类型是 id ,所以编译器会发出警告,提示该方法可能无法实现。
ConcreteRequest.m:38:警告: 属性“委托”需要方法 '-delegate' 被定义 - 使用 @synthesize、@dynamic 或提供 方法实现
问题
此警告是有道理的,因为该属性毕竟是键入 id
的。为了解决这个问题,我想向编译器明确指出分配给具体实例的委托必须是 id
类型。这对我来说听起来完全合理,所以我在 ConcreteRequest 标头中添加了一个新属性,希望覆盖抽象属性:
@property (nonatomic, assign) id <ConcreteRequestDelegate> delegate;
但这就是编译器不同意我的地方,可能有充分的理由。我本以为它会发出警告,以错误的类型覆盖超类的属性,但它只是要求我重新合成这个新属性。我不想去那里,因为这样超类的方法将无法访问相同的委托属性。
问题
有没有办法使用添加的类型信息“重新声明”具体子类中的属性?或者你能发现我的想法中的错误吗?因为也许这是一个相当普遍的问题,我直到现在才遇到?
干杯,
EP。
PS 本作品中出现的所有类和协议名称都是虚构的。任何与真实类和协议名称(开源或专利)的相似之处纯属巧合。
The Scenario
I have a situation where a base class called AbstractRequest
has a delegate property of type id <AbstractRequestDelegate>
declared in the header file:
@property (nonatomic, assign) id <AbstractRequestDelegate> delegate;
The abstract delegate protocol contains a few required methods, and as indicated with the word 'abstract', both the AbstractRequest
and the AbstractRequestDelegate
are intended to be subclasses/extended.
One example of this would be the subclass ConcreteRequest and extended protocol ConcreteRequestDelegates, that both add extra methods to the abstract ones. The intention is that both the abstract and concrete class methods can send messages to the single assigned delegate instance.
At a certain point in time the ConcreteRequest would like to call a method on the delegate that is defined by ConcreteRequestDelegate. Because the type of the delegate is id , the compiler will give a warning that this method might not be implemented.
ConcreteRequest.m:38: warning:
property 'delegate' requires method
'-delegate' to be defined - use
@synthesize, @dynamic or provide a
method implementation
The Problem
This warning is justified, for the property is after all typed to id <AbstractRequestDelegate>
. In order to fix this, I want to make clear to the compiler that the delegate assigned to the concrete instance must be of type id <ConcreteRequestDelegate>
. This sounded perfectly reasonable to me, so I put in a new property in the ConcreteRequest header, hoping to override the abstract one:
@property (nonatomic, assign) id <ConcreteRequestDelegate> delegate;
But this is where the compiler disagrees with me, probably with good reason. I would have thought it would give a warning for overriding a super class' property with the wrong type, but instead it just demands me to re-synthesize this new property. I don't want to go there, because then the super class' methods won't have access to the same delegate property.
The Question
Is there a way to 're-declare' the property in the concrete subclass with the added type information? Or can you spot the error in my thinking, for maybe this is a fairly common problem that I just haven't come across until now?
Cheers,
EP.
P.S. All class and protocol names appearing in this work are fictitious. Any resemblance to real class and protocol names, open source or patented, is purely coincidental.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
警告已经给出了正确的线索。我在重写子类中使用了@dynamic,一切都很好。
The warning already gave the right clue. I used @dynamic in the overriding subclass and all is good.
只需在
ConcreteRequest.m
中合成iddelegate
即可正常工作......不会产生任何问题。Just synthesize
id<ConcreteRequestDelegate>delegate
in theConcreteRequest.m
it will work fine...It won't create any problem.