如何用更具体的类型覆盖超类的属性? [英] How to override a superclass' property with more specific types?

查看:108
本文介绍了如何用更具体的类型覆盖超类的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景
我遇到的情况是,名为AbstractRequest的基类具有在头文件中声明的类型为id <AbstractRequestDelegate>的委托属性:

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;

抽象委托协议包含一些必需的方法,并且如单词抽象"所示,AbstractRequestAbstractRequestDelegate均旨在作为子类/扩展类.

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.

一个例子是子类ConcreteRequest和扩展协议ConcreteRequestDelegates,它们都向抽象方法添加了额外的方法.目的是抽象类和具体类方法都可以将消息发送到单个分配的委托实例.

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.

在特定的时间点,ConcreteRequest希望在由ConcreteRequestDelegate定义的委托上调用一个方法.因为委托的类型是id,所以编译器将警告该方法可能无法实现.

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:警告: 属性委托" Requires方法 要定义的"-delegate"-使用 @ synthesize,@ dynamic或提供一个 方法实现

ConcreteRequest.m:38: warning: property 'delegate' requires method '-delegate' to be defined - use @synthesize, @dynamic or provide a method implementation

问题
此警告是合理的,因为该属性毕竟键入了id <AbstractRequestDelegate>.为了解决此问题,我想向编译器表明分配给具体实例的委托必须为id <ConcreteRequestDelegate>类型.这对我来说听起来完全合理,所以我在ConcreteRequest标头中添加了一个新属性,希望覆盖抽象的属性:

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?

干杯,
EP.

Cheers,
EP.

P.S.出现在这项工作中的所有类和协议名称都是虚构的.与真实的类和协议名称(开源或已获专利)的任何相似之处纯属巧合.

推荐答案

警告已给出正确的线索.我在覆盖的子类中使用了@dynamic,一切都很好.

The warning already gave the right clue. I used @dynamic in the overriding subclass and all is good.

这篇关于如何用更具体的类型覆盖超类的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆