LEARN π | Example of Delegate Design Pattern in Swift 5.1
This is the Delegate representation as a "cake from pan". Metaphorically, is: How the calls between two people needs to be?
protocol AdvanceLifeSupport{
func peformCPR()
}
class EmergencyCallHandler{
var delegate:AdvanceLifeSupport?
func assesSituation(){
print("Can you tell what happened?")
}
func medicalEmergency(){
delegate?.peformCPR()
}
}
We need to initialize the "Delegate" to be able to this this "Struct" receive calls
struct parametric:AdvanceLifeSupport{
init(handler: EmergencyCallHandler) {
handler.delegate = self
}
func peformCPR() {
print("The parametric does chest compression, 30 per second")
}
}
When Peter call medicalEmergency ,Joao will receive this and start performCPR as does he knows
let peter = EmergencyCallHandler()
let joao = parametric(handler: peter)
peter.medicalEmergency()
When can use to everything, especially Http Request with parallel threads to be communicated to Views, furthermore is very comma use delegates in TextViews and TableViews! Like:
The ViewController has a tableView and we initialize it
@IBOutlet weak var tableView: UITableView!
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
tableView.delegate = self
}
}
This will trigger the tableView to change the data of it.
//MARK: - ChatTableViewDataSource
extension ChatViewController:UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//return the number of cells of the tables
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//return the cell content
}
}
Besides that, we create a extension to separate our code and make it more organized