I am new to iOS, don't know if this is possible or not.
Basically I have two classes Parent and Child.
Parent has a delegate which conforms to ParentProtocol. However, the delegate in Child not only conforms to ParentProtocol, but also another ChildProtocol.
So is it possible to do the following?
@interface Parent {
@property (nonatomic, unsafe_unretained) id<ParentProtocol> delegate;
}
@interface Child : Parent {
@property (nonatomic, unsafe_unretained) id<ParentProtocol, ChildProtocol> delegate;
}
Yes, this is valid code. This amounts to declaring the methods
in the
Parent
interface and declaring methods for the same selectors (-delegate
and-setDelegate
) in theChild
interfaceThis is permissible (and causes no warnings) because
id<ParentProtocol>
andid<ParentProtocol, ChildProtocol>
are compatible types. (Contrast this to the situation where inChild
's declaration you declaredelegate
to be of typeNSArray *
. You'll get the warningProperty type 'NSArray *' is incompatible with type 'id<ParentProtocol>' inherited from 'Parent'
.)By the way, it is worth noting that you can define
ChildProtocol
to inherit fromParentProtocol
by writingand
Which in turn would allow you to write in the interface of
Child
rather than