Here’s an open source library from Alec Larson providing a Swift based alternative to NSNotificationCenter with a very clean and simple syntax.
This example from the readme shows how simple it is to create a generic emitter with EmitterKit:
let didLogin = Event() didLogin.once { user in println("Successfully logged in as \(user.name)!") } didLogin.emit(user)
The advantages of EmitterKit as stated in the readme include:
- No longer be forced to typecast event data in your closures.
- No longer be forced to removeObserver in your classes’ deinit methods.
- Backwards-compatible with NSNotificationCenter (like UIKeyboardWillShowNotification)!
- Simpler syntax
You can find Emitter-Kit on Github here.