Delegation

Delegation is one of the most powerful features of protocols. Delegation is not special to Swift or Objective-C. It is a design pattern, a reusable solution to a common problem within a certain context. A friend of mine described delegation best without even knowing it. He said, “Doesn’t a protocol allow you to send messages to anyone who implements that protocol?” Sort of. If you create a new iOS project, the first thing you will most likely see is the AppDelegate, which notifies you when certain things happen. For example, in AppDelegate there is a method called applicationWillTerminate. This method gets called when the application is about to be shut down. It can be called because it adopts UIApplicationDelegate. Let’s look at ...

Get Learning Swift™ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.