Swift 4.0 & Xcode 9.0+:
Send (Post) Notification:
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil)
OR
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil, userInfo: ["Renish":"Dadhaniya"])
Receive (receive) a notification:
NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil)
The handler of the function method for the received notification:
@objc func methodOfReceivedNotification(notification: Notification) {}
Swift 3.0 & Xcode 8. 0+:
Send (Post) Notification:
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil)
Receive (receive) a notification:
NotificationCenter.default.addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil)
Method handler for received notification:
func methodOfReceivedNotification(notification: Notification) {
Delete notification:
deinit { NotificationCenter.default.removeObserver(self, name: Notification.Name("NotificationIdentifier"), object: nil) }
Swift 2.3 & Xcode 7:
Send (Post) Notification
NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)
Receive (receive) notifications
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(_:)), name:"NotificationIdentifier", object: nil)
Method handler for received notification
func methodOfReceivedNotification(notification: NSNotification){
Ref: https://medium.com/@javedmultani16/notification-in-swift-4-8b0db631f49d