thank_you
thank_you

Reputation: 11107

Swift - unrecognized selector sent to instance

I have a simple NSNotification set up on my Swift project.

NSNotificationCenter.defaultCenter().addObserver(self, selector: "serviceAccessChanged", name:"LocationAccessChangedNotification", object: nil)

I've also tried...

NSNotificationCenter.defaultCenter().addObserver(self, selector: "serviceAccessChanged:", name:"LocationAccessChangedNotification", object: nil)

The method called looks like so.

private func serviceAccessChanged() {
    println("serviceAccessChanged")
}

When the notification is made I receive the following error.

-[CoolApp.HomeViewController serviceAccessChanged]: unrecognized selector sent to instance 0x7fc91324bba0

What's wrong and how do I fix this?

Upvotes: 1

Views: 801

Answers (2)

yshilov
yshilov

Reputation: 794

Just mark your private method with @objc

@objc private func serviceAccessChanged() {
    println("serviceAccessChanged")
}

Upvotes: 1

Léo Natan
Léo Natan

Reputation: 57060

Private functions are not exposed to Objective C, and this is why you get this exception. Make this method accessible, and use the serviceAccessChanged selector.

Upvotes: 3

Related Questions