Swift 4 - addObserver notification center

I crash and get an unrecognized selector error every time a Notification arrives and the application tries to execute the method associated with it. Here is my code - which is in viewDidLoad :

 let notificationCenter = NotificationCenter.default notificationCenter.addObserver(self, selector: Selector(("sayHello")), name:NSNotification.Name(rawValue: "dataDownloadCompleted"), object: nil) 

The sayHello() method is quite simple - it looks like this:

 func sayHello() { print("Hello") } 

I checked that the Notification sent successfully and that it arrives successfully - so this is not a problem. A crash occurs when the application looks like to start Notification by running the sayHello() method. It keeps giving me an unrecognized selector error.

Any ideas what I'm doing wrong? (By the way, this worked fine with Swift 3 and Xcode 8, but now with Swift 4 and Xcode 9 the syntax has changed [Xcode led me through the necessary fixes / updates to the code] - but crashes continue to occur.)

+11
source share
3 answers

You can improve your code with the following steps:

 extension Notification.Name { static let dataDownloadCompleted = Notification.Name( rawValue: "dataDownloadCompleted") } 

And use it as follows:

 let notificationCenter = NotificationCenter.default notificationCenter.addObserver(self, selector: #selector(YourClass.sayHello), name: .dataDownloadCompleted, object: nil) 

But, as already mentioned, the problem is solved by changing to #selector

+29
source
 Data Receiving - Add observer: override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(yourfunction(notfication:)), name: .postNotifi, object: nil) } @objc func yourfunction(notfication: NSNotification) { print("xxx") } Sending Data - Post Notification: override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) NotificationCenter.default.removeObserver(self, name: .postNotifi, object: nil) } extension Notification.Name { static let postNotifi = Notification.Name("postNotifi") } 
+7
source

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) { // Take Action on 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){ // Take Action on Notification } 

Ref: https://medium.com/@javedmultani16/notification-in-swift-4-8b0db631f49d

0
source

Source: https://habr.com/ru/post/1272529/


All Articles