NSNotificationCenter has been around since the dawn of the iOS SDK and it’s been a popular tool amongst developers for just as long. For those who dont know, it’s based off the concept conveniently named “Observer pattern”, which is part of the behavioural group of software design patterns.
extension Selector {
static let makeCoffeeNotification = #selector(Customer.drink(_:))
}
class Customer {
@objc func drink(notification: NSNotification) {
print("Mmm... Coffee")
}
}
let customer = Customer()
Barista.addObserver(customer, selector: .makeCoffeeNotification, notification: .makeCoffee)
Barista.postNotification(.makeCoffee)
// prints: Mmm... Coffee
Barista.removeObserver(customer, notification: .makeCoffee)
Swift Functional Programming: Kinda like reverse Fight Club for developers