More easier to use NotificationCenter.
No need to define Notification Name string, no worries about typo
NotificationCenter.default.addObserver(self, selector: #selector(handleNotification(_:)), name: NSNotification.Name(rawValue: "UserLogin"), object: nil)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "UserLogin"), object: true, userInfo: ["Name": "xxxx"])
override func handleNotification(_ notification: Notification) {
let success = notification.object as? Bool ?? false
let name = notification.userInfo?["name"] as? String ?? "" //!!!should be 'Name', not 'name'
...
}
Emm...
public enum NotifierName: String {
case login
case logout
case registration = "NotificationKeyRegistration"
...
}
public enum NotifierKey: String {
case result
case name
...
}
Notifier.register(.login, to: self, selector: #selector(handleNotification(_:)))
Support Notification.Name:
Notifier.register(.UIKeyboardWillShow, to: self, selector: #selector(handleKeyboardWillShow(_:)))
Notifier.unregister(.login, from: self)
Notifier.post(.login, value: [.result: true, .name: "god"])
let success: Bool = notification.value(for: .result) ?? false
let name: String = notification.value(for: .name) ?? ""
print("notification: \(success) ---- \(name)")