Whenever I create an init that has a delegate conforming to a protocol I write the init as this:
- (id)initWithDelegate:(id<ProtocolToConform>)delegate;
This way I will have a warning if the creating object does not conform to the protocol.
However I noticed that ie UIAlertView init method looks like this:
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
The delegate argument does not specify to conform to the UIAlertViewDelegate protocol? Any clues why Apple has done it that way?
My understanding is that the API was a big mess a few years ago and they are still fixing all these issues.
Note that until recently many protocols were informal and almost everywhere in the API you could find delegates only as an
id. Apple is fixing it step by step in every version, for example formalNSURLConnectiondelegates came with iOS 6.0. Or note that in iOS 6.1 some of theidreturn types were changed intoinstancetype.Well, this was my initial idea, after checking the header files:
It’s obvious somebody wanted to add the protocol to the declarations but added only the comment. I am guessing they left it without the protocol for compatibility.