0% found this document useful (0 votes)
20 views3 pages

Understanding The Delegate Pattern in Swift

Uploaded by

Amazing Deals
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Understanding The Delegate Pattern in Swift

Uploaded by

Amazing Deals
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Understanding the Delegate Pattern in Swift

What Is Delegation?

 A design pattern where one object (delegator) assigns tasks or responsibilities to another
(delegate).
 Enables communication between objects in a decoupled manner.
 Achieved using protocols in Swift (similar to interfaces in other languages).

Metaphor Example

 Manager needs tasks done (e.g., laundry, reporting).


 Writes a "job description" = protocol.
 Assistant = the delegate, signs the contract by adopting the protocol.
 Manager calls on the assistant to perform the tasks, without caring who the assistant is, as
long as they follow the protocol.

Why Use Delegation?


Benefits:

 Separation of concerns: Business logic and helper logic are in different classes.
 Loose coupling: Classes interact via protocols, not direct references.
 Code reuse: Easily swap out delegate implementations.
 Testability: Each class can be tested in isolation.
 Team collaboration: Multiple developers can work on different components
independently.

Real-World Usage in iOS


 Commonly seen in UITableView, UICollectionView, UITextField, etc.
 Apple frameworks rely heavily on delegation for custom behavior injection.
 UIKit components delegate responsibilities like event handling, data provision, etc.
Custom View Example with Delegation
1. Define the Delegate Protocol
protocol SomeDetailViewDelegate: AnyObject {
func didTapButton()
}

2. UIView Subclass Uses Delegate


class SomeDetailView: UIView {
weak var delegate: SomeDetailViewDelegate?
private let button = UIButton()

// Configure the button


button.addTarget(self, action: #selector(didTapButton),
for: .touchUpInside)

@objc private func didTapButton(_ sender: UIButton) {


delegate?.didTapButton()
}
}

3. UIViewController Sets Itself as the Delegate


class SomeDetailViewController: UIViewController {
private lazy var detailView: SomeDetailView = {
let view = SomeDetailView()
view.delegate = self
return view
}()
}

4. Conform to the Protocol


extension SomeDetailViewController: SomeDetailViewDelegate {
func didTapButton() {
print("Do something when the user taps the button")
}
}

Key Concepts Summary


Concept Description
Defines the contract (what tasks the delegate must perform).
Protocol
The object that performs the task (must conform to the protocol).
Delegate
Concept Description
The object that assigns the task (calls the delegate’s methods).
Delegator
Loose
Delegator doesn’t care about the delegate’s type, only that it meets the contract.
Coupling

Weak Used to avoid strong reference cycles (commonly used for delegate properties).
Reference

Delegation allows code components (like views) to be reused with different


Reuse
controllers or handlers.

You might also like