Skip to content

samuel-mellert/Blog_NSNotificationCenterProtocol

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Swift: NSNotifcationCenter protocol

Observer pattern made better-er

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)

Read more

Swift Functional Programming: Kinda like reverse Fight Club for developers

About

A Medium blog post for NSNotificationCenter

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Swift 100.0%