简体   繁体   English

输入id <Protocol1> 不符合id <Protocol2> - 但确实如此!

[英]Type id <Protocol1> does not conform to id <Protocol2> — but it does!

Alright, I have two protocols in the same header file, let's call them Protocol1 and Protocol2. 好吧,我在同一个头文件中有两个协议,让我们称它们为Protocol1和Protocol2。 I have a main app controller that conforms to both protocols, and an NSWindowController subclass that has the following member: 我有一个符合这两个协议的主app控制器,以及一个具有以下成员的NSWindowController子类:

id <Protocol1, Protocol2> delegate;

I'm getting a warning at the end of my NSWindowController subclass implementation that "type id does not conform to Protocol2". 我在我的NSWindowController子类实现结束时收到“类型id不符合Protocol2”的警告。 But, as shown, the delegate must conform to both protocols, which it does. 但是,如图所示,委托必须符合这两种协议。

Furthermore, the application works perfectly. 此外,该应用程序完美。 Is there some other way to do this? 还有其他方法可以做到这一点吗? I suppose I could just fold the two protocols in together, but that would hurt the modularity of the program. 我想我可以将两个协议一起折叠,但这会损害程序的模块性。

EDIT: 编辑:

Here are the two protocols. 这是两个协议。 As this is more of a test scenario, they're short. 由于这更像是一个测试场景,因此它们很短。

@protocol TPTBController <NSObject>

-(void)sendGrowlMessage:(NSString *)message title:(NSString *)title;

@end

@protocol AddPower <NSObject>

-(void)addPower:(NSArray *)array;
-(void)setCanAddPower:(BOOL)can;

@end

The language spec isn't clear if the id-with-protocols actually supports a protocol list or not. 如果id-with-protocols实际上支持协议列表,则语言规范不明确。 Protocols can extend protocol lists, but it's not clear if that syntax supports it or not. 协议可以扩展协议列表,但不清楚该语法是否支持它。

You could create a combined protocol: 您可以创建组合协议:

@protocol AddPowerAndTPTBController <AddPower, TPTBController>
@end
...
id <AddPowerAndTPTBController> delegate;

Whilst not elegant, it would work; 虽然不优雅,但它会起作用; but it would require your delegate class to conform to the AddPoewrAndTPTBController as well, not just the two individually. 但它需要你的委托类也要符合AddPoewrAndTPTBController,而不仅仅是两个单独的。

Are you importing the protocols on your NSWindowController subclass? 您是否在NSWindowController子类上导入协议?

That the application works points me in that direction. 应用程序的工作指向了我的方向。 It seems that when doing the static checking, the compiler can't figure that your class conforms to the protocols, while when actually dispatching messages it's succeeding (and that's why the application works as expected) 似乎在进行静态检查时,编译器无法确定您的类是否符合协议,而在实际调度消息时它正在成功(这就是应用程序按预期工作的原因)

如果将协议拆分为单独的文件,然后将它们都导入NSWindowController类会发生什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM