1

I have a doubt about classes that designate a delegate.

Sometimes the delegate object needs to implement a protocol, sometimes not. For example, if you want to display an UIAlertView, you create it specifying a delegate like self. Then, the class that is calling the UIAlertView must implement the UIAlertViewDelegate protocol.

If you create other classes like NSUrlConnection with the connectionWithRequest:delegate: message, you designate a delegate, which will receive the delegate messages sent from NSUrlConnection as the connection progresses, but the object designated as a delegate do not have to implement a protocol.

Am I missing something? How do I understand when I should implement a protocol? Is it always clear from the API reference as it is for UIAlertViewDelegate?

I've noticed that UIAlertView reference only presents instance methods and tells you that you have to implement the UIAlertViewDelegate protocol, while NSUrlConnection does not talk about protocols to implement, but it has a delegate methods section.

Could NSUrlConnection have been defined like the UIAlertView class, without delegate methods but with a NSUrlConnectionDelegate protocol.

Or am I missing the point? It seems to me two different ways for similar purposes, but maybe I am wrong. I hope I've been quite clear, I just want to fully understand the rationale behind this stuff.

APC
  • 144,005
  • 19
  • 170
  • 281
Gianni Costanzi
  • 6,054
  • 11
  • 48
  • 74

1 Answers1

0

NSURLConnection itself implements the delegate methods, providing some default behaviors for the those events.

UIAlertView, by default, simply does not handle those events. And allow you to make a delegate to customize the handling.

The difference is whether default behaviors are needed.

XMLSDK
  • 259
  • 1
  • 10
  • But the methods in the Delegate Methods section in the API Reference are "standard methods" which are simply designated as Delegate Methods to make you (the developer) understand that they can be implemented by the delegate object? So the rationale is 1) if the class itself implements something but allows its delegate to override the default behavior, then the protocol is not needed while 2) if the class itself does not implement a default behavior and you can/must implement some methods for the designated delegate class, then you will have to conform to a protocol, is it right? – Gianni Costanzi Sep 03 '11 at 09:15
  • Please refer to Protocols and Categories: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html Categories could provide extra methods to the class, you may in the delegate case, imagine that as the default implementation. Protocols may ask you to implement optional or required methods. NSURLConnectionDelegate is defined as Category (its API Reference mark those methods as 'delegate method'), while UIAlertViewDelegate is defined as a Protocol. – XMLSDK Sep 03 '11 at 09:33
  • Your stated rationale is correct. And both Protocols and Categories help make less subclassing. – XMLSDK Sep 03 '11 at 09:41
  • Uh... I didn't see that there was an [NSURLConnectionDelegate protocol](http://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html)... this is confusing me... is the delegate object specified in connectionWithRequest:delegate: message of NSURLConnection required to implement the protocol NSURLConnectionDelegate (i.e. does the connectionWithRequest:delegate: method check if the delegate object conforms to that protocol)..? – Gianni Costanzi Sep 03 '11 at 09:56
  • NSURLConnectionDelegate is not a protocol. Navigate to NSURLConnectionDelegate definition in Xcode (cmd+double click). You could see it's in NSURLConnection.h and is a Category of NSObject. – XMLSDK Sep 03 '11 at 10:04
  • We don't know Apple's source code but NSURLConnection may use `respondsToSelector:` to check whether the delegate object implements the methods. – XMLSDK Sep 03 '11 at 10:08
  • I'm sorry, I said it was a protocol because it was what I've read on the [reference page](http://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html) – Gianni Costanzi Sep 03 '11 at 10:42
  • NSURLConnectionDelegate (as a Protocol) only exists in Mac v10.7 or later. For iOS and Mac v10.6, it implements as a Category of NSObject. – XMLSDK Sep 03 '11 at 10:50
  • Look at its revision history, it's added on 2011-05-04 for Mac v10.7. That protocol page does not exist in the older document! – XMLSDK Sep 03 '11 at 10:56
  • That means in Mac v10.6, NSURLConnectionDelegate is an informal protocol (Informal protocols are typically declared as categories of the NSObject class), and become a formal protocol in v10.7 – XMLSDK Sep 03 '11 at 11:03
  • But since you tag ios, you should refer to ios's reference and definition header NSURLConnection.h of iOS 4.3. For iOS 5.0 we cannot talk here......but you may guess what also happened.... – XMLSDK Sep 03 '11 at 11:15
  • Thank you very much for the clear explaination... excuse me if I posted some trivial questions, I'll pay more attention to the docs. – Gianni Costanzi Sep 03 '11 at 11:21
  • This site is very nice, full of people that's ready to help you ;-) – Gianni Costanzi Sep 03 '11 at 18:20