目标C中的协议继承 [英] Protocol inheritance in Objective C

查看:62
本文介绍了目标C中的协议继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,其中包含一个协议,一个实现该协议的类以及一个实现类的子类.这是我们的生产应用程序.

I've got a project which has in it a protocol, a class implementing that protocol, and a subclass of the implementation class. This is our production application.

@protocol ProductionProtocol<NSObject>
@property (nonatomic, retain) NSString *role;
@end

@interface BaseProduction : NSObject<ProductionProtocol>
NSString *role;
@end

@implementation BaseProduction
@synthesize role;
@end

@interface Production : BaseProduction
@end

@implementation Production
@end

我还获得了概念验证(POC)应用程序,该应用程序作为包含生产应用程序的单独项目实现.在POC应用程序中,我有一个扩展生产协议的协议和一个扩展生产类的类.

I've also got a proof of concept (POC) application, which is implemented as a separate project that includes the production application. In the POC application, I have a protocol that extends the production protocol, and a class that extends the production class.

@protocol POCProtocol<ProductionProtocol>
-(void)cancel;
@end

@interface POC : Production<POCProtocol>
@end

@implementation POC
-(void)cancel{...}
@end

请注意,在ProductionProtocol中,我有一个NSString角色,该角色已声明并在BaseProduction接口/类中实现.在POC中,我有一个方法"cancel",该方法已在协议中声明,但未在接口/类中声明.

Notice that in the ProductionProtocol, I've got a role NSString which is declared, and implemented in the BaseProduction interface/class. in the POC, I've got a method 'cancel' which is declared in the protocol, but not in the interface/class.

所以这是我的问题:通过这样的类结构设置,我得到以下警告:

So here's my question: with my class structure set up like this, I get this warning:

Property 'role' requires method '-role' to be defined - use @synthesize, @dynamic or provide a method implementation

我不明白为什么收到此警告.由于综合属性位于基类中,因此POC类应该可以使用它们-快速测试似乎可以确认它们是否存在.那么,我在这里做错了什么?

I don't understand why I'm getting this warning. Since the synthesized properties are in the base class, they should be available to the POC class - and a quick test seems to confirm that they are. So what am I doing wrong here?

推荐答案

Objective-C尚无官方语言定义,但根据

There is no official language definition for Objective-C, but according to Apple:

当类采用协议时,它必须实现协议声明的必需方法,如前所述.另外,它必须符合所采用协议所包含的任何协议.如果合并的协议还包含其他协议,则该类也必须符合它们.使用以下任何一种技术,一类都可以符合合并的协议:

When a class adopts a protocol, it must implement the required methods the protocol declares, as mentioned earlier. In addition, it must conform to any protocols the adopted protocol incorporates. If an incorporated protocol incorporates still other protocols, the class must also conform to them. A class can conform to an incorporated protocol using either of these techniques:

  • 实现协议声明的方法
  • 从采用协议并实现方法的类继承

但是,有报告称GCC无法识别该属性在合并的协议中,并且是在超类中实现的.您可以将编译器更改为Clang,据报告可以使用指定的方式处理此问题,也可以仅使用@dynamic告诉编译器将在运行时提供该属性的实现(在这种情况下,通过继承)来自超类).

However, reports are that GCC doesn't recognize that the property is in an incorporated protocol and is implemented in a superclass. You could change your compiler to Clang, which is reported to handle this in the specified manner, or you could just use @dynamic to tell the compiler that an implementation of the property will be provided at run time (in this case, by inheritance from the superclass).

这篇关于目标C中的协议继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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