Understanding Delegates and Protocols
Earlier chapters introduced two ideas:
Protocols. A protocol is a bundle of optional methods that can be “adopted”—or used in—any object.
Delegates. A delegate is an object that handles optional messages. In practice, delegates typically include code that makes protocol methods do something useful.
Many frameworks use protocols and delegates, because they’re a convenient way to implement certain kinds of features.
Understanding delegates
Officially, a delegate is an object that handles a message on behalf of some other object. But this definition is misleading in practice. Objects often delegate messages to themselves. If you don’t know how delegates are used in iOS, it’s difficult to understand why.
In practice, delegate messages are usually used to add extra code that runs when something significant is about to happen, something has already happened, or iOS is asking for data and/or an object. This extra code is usually optional, and you can build a useful app without it. But it provides some extra “hooks” you can use to make your app behave in a more sophisticated and useful way.
“Something just happened” can mean “the user did something” ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access