- Notifications
- Delegation
- Callbacks
Notifications
NotificationCenter.default.addObserver(self, selector: #selector(onDidReceiveData(_:)), name: .didReceiveData, object: nil)
@objc func onDidReceiveData(_ notification:Notification) {
// Do something now
}
Observer
NotificationCenter.default.addObserver(self, selector: #selector(onDidReceiveData(_:)), name: .didReceiveData, object: nil)
Delegation
protocol ModelDelegate: class {
func didReceiveData(_ data: String)
}
Callbacks
func getBoolValue(number : Int, completion: (result: Bool)->()) {
if number > 5 {
completion(result: true)
} else {
completion(result: false)
}
}
getBoolValue(8) { (result) -> () in
// do stuff with the result
print(result)
}
Important to understand:
(String)->() // takes a String returns void
()->(String)