The official Apple documentation defines delegation as:
Delegation is a design pattern that enables a class to hand off (or “delegate”) some of its responsibilities to an instance of another class.
That’s quite complex, so let’s break it down…
Think about delegation in the real world. Imagine you and I are part of a team that delivers chocolate cookies to an event. You’re in charge of baking cookies, and you delegate making the cookie dough to me. Once I’m done I give the cookie dough to you, so you can use it to bake the cookies.
A few key points stand out:
- You’re in charge of making cookies, and you delegate creating cookie dough to me
- You could say that making cookies is your responsibility, and you’re handing off some of that responsibility to me
- And it goes two ways: I give you the cookie dough once I’m done with my delegated task
It’s not much different in Swift programming! One class delegates a task to another class, handing off some of its responsibilities. This enables the delegate to customize the base class, as we’ll soon find out.
learnappmaking.com/delegation-swift-how-to/#what-is-delegation
'Programming > Swift' 카테고리의 다른 글
window (0) | 2020.10.16 |
---|---|
UIStackView (0) | 2020.10.15 |
Class: UIControl (0) | 2020.10.14 |
Difference between viewdidappear and viewwillappear in swift (0) | 2020.10.14 |
IBAction and IBOutlet (0) | 2020.10.14 |