是的,这是有效的代码。这相当于声明方法
- (id<ParentProtocol>)delegate;
- (void)setDelegate:(id<ParentProtocol>)delegate;
在接口中并在接口中为相同的选择器(和)Parent
声明方法-delegate
-setDelegate
Child
- (id<ParentProtocol, ChildProtocol>)delegate;
- (void)setDelegate:(id<ParentProtocol, ChildProtocol>)delegate;
这是允许的(并且不会导致警告),因为id<ParentProtocol>
andid<ParentProtocol, ChildProtocol>
是兼容的类型。Child
(这与在's 声明中声明delegate
为 type的情况形成对比NSArray *
。你会收到警告Property type 'NSArray *' is incompatible with type 'id<ParentProtocol>' inherited from 'Parent'
。)
顺便说一句,值得注意的是,您可以通过编写来定义ChildProtocol
继承自ParentProtocol
@protocol ParentProtocol <NSObject>
//....
@end
和
@protocol ChildProtocol <ParentProtocol>
//....
@end
反过来,这将允许您在Child
@property (nonatomic, unsafe_unretained) id<ChildProtocol>;
而不是
@property (nonatomic, unsafe_unretained) id<ParentProtocol, ChildProtocol>;