简体   繁体   中英

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. I have a main app controller that conforms to both protocols, and an NSWindowController subclass that has the following member:

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". 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.

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. 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.

Are you importing the protocols on your NSWindowController subclass?

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类会发生什么?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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