Basic question about delegates


When defining a delegate, why not declare that the delegate’s class conforms to the particular delegation protocol? Why, instead, just define methods that a part of the protocol? Is this because most delegation protocols are “informal” (i.e. defined using Objective-C categories instead of actual protocols)? It would seem to be a lot less error prone to actually conform to a protocol since the method signatures could be checked at compile time.


In many cases (increasingly so) classes must specify that they conform to a protocol in the @interface. This is not presently checked by Interface Builder, however, but it would show up as an error if you were setting a class to be the delegate of another without implementing its delegate protocol.

This will help in cases where a delegate protocol has required methods, but not for optional methods, and the vast majority of delegate methods are optional (that’s part of what’s nice about the delegation pattern - you can choose what you want to be notified about, or what behavior you want to adjust).

Is that helpful?



Yes, that does help. But, I thought it was now possible to define some methods as “optional” within a protocol. I can see how informal protocols were essential (i.e. to allow for optional methods) before Objective-C 2.x, but it seems like going forward defining formal protocols with “optional” sections is a better approach.