George Vardikos
George Vardikos

Reputation: 2437

NSNotification issue - unrecognized selector sent to instance

I have this observer

NotificationCenter.default.addObserver(self, selector: #selector(flashButtonDidPress(_:)), name: NSNotification.Name(rawValue: "flash"), object: nil)

And this delegate function

func flashButtonDidPress(_ title: String) {
    cameraController.flashCamera(title)
}

Can someone explain me why I have the following error?

unrecognized selector sent to instance

Thanks in advance

EDIT: I am also accessing the function without the use of a notification

Upvotes: 1

Views: 836

Answers (1)

vadian
vadian

Reputation: 285074

NotificationCenter sends Notifications, not Strings, use a second function to be called from somewhere else:

func flashButtonDidPress(_ notification: Notification) {
   if let title = notification.userInfo?["title"] as? String {
      flashCamera(with:title)
   }
}

func flashCamera(with title: String)
{
  cameraController.flashCamera(title)
} 

pass the title in the userInfo dictionary when posting the notification, e.g.

let userInfo = ["title", title]

Upvotes: 3

Related Questions