Questions about why (no) NSURLConnectionDelegate Protocol


#1

Three questions really…

  1. Why don’t we have ListViewController.h conform to NSURLConnectionDelegate protocol?
  2. Also I noticed that the NSURLConnectionDelegate Protocol is different for OS X (v10.7 and later) and for iOS (5.0 and later). The connection:didReceiveData: is listed under the protocol reference for OS X (as required), but not for iOS. Is this a case of where the protocol just hasn’t caught up with the connection implementation?
  3. Finally, why don’t we set these delegates to nil in the dealloc: method of ListViewController.h?

#2
  1. Probably because all methods from NSURLConnectionDataDelegate and NSURLConnectionDelegate are optional.

#3
  1. Probably should, I think that may have been an accidental omission.

  2. Yeah, there are now three protocols for NSURLConnection: NSURLConnectionDownloadDelegate, NSURLConnectionDataDelegate and NSURLConnectionDelegate.

The first is for streaming the data to disk, the second to memory and the third is a “abstract base protocol” that the first two “inherit” from, i.e., the common methods to both.

  1. Oddly enough, an NSURLConnection actually keeps a strong reference to its delegate. The controllers won’t ever be deallocated while the connections live.

#4

Given that we have not declared that ListViewController conforms to NSURLConnectionDelegate protocol, why does having the ListViewController pass itself for a NSURLConnectionDelegate parameter not generate an error?


#5

To be more specific, I’m referring to the following line on page 442:

Is declaring that a class conforms to a protocol unnecessary if the methods in the protocol are all optional? That would seem odd.


#6

I found that Xcode would auto-complete if I declared the delegate, but would not if I did not (as expected). But, yeah, it works without the declaration.

The fourth edition does have the declaration in the new Chapter 21.

I guess we can look at it this way, if you declare yourself a delegate, you must implement all the non-optional methods. But, since NSURLConnectionDelegate doesn’t have any non-optional methods, anything can be an NSURLConnectionDelegate.

-Craig