Chapter    47

Delegation

Delegation is a design pattern where one object asks another object for help. Protocols (Chapter 46) are an important part of delegation because protocols define how an object will be helped.

Delegation works by defining a protocol that will list all the methods and properties an object will need help with. Another object, known as the delegate, will provide the help needed by adopting and implementing the protocol. Objects ask for help by sending messages to their delegates.

To demonstrate delegation, imagine that you are working on a project management application and you’ve defined two classes: Project and Task. A Project instance manages a list of Task instances (see Listing 47-1).

Listing 47-1. Project Manager Classes ...

Get Swift Quick Syntax Reference 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.