Object X of Class Y Does Not Implement Methodsignatureforselector in Swift

Object X of class Y does not implement methodSignatureForSelector in Swift

Don't think of NSObject as an Objective-C class, think of it as a Cocoa/Foundation class. Even though you're using Swift instead of Objective-C, you're still using all the same frameworks.

Two options: (1) add the dynamic attribute to the function you want to reference as a selector:

    dynamic func timerTick() {
self.angerLevel++
print("Angry! \(self.angerLevel)")
}

Or (2) declare Person as a subclass of NSObject, then just call super.init() at the beginning of your initializer:

class Person: NSObject {
var timer = NSTimer()
var angerLevel = 0

func startTimer() {
print("starting timer")
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timerTick", userInfo: nil, repeats: true)
}

func timerTick() {
self.angerLevel++
print("Angry! \(self.angerLevel)")
}

override init() {
super.init()
self.startTimer()
}
}

UIButton click crashing does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector

The problem as I discovered was that MyController class need to inherit from NSObject class. Changing the class declaration to as following fixed my problem.

class MyController : NSObject
{
override init() // since it is overriding the NSObject init
{
}
}

This is probably because NSObject implements methods like respondsToSelector. And before calling the pressed: function it tries to check if it infact implements the selector pressed:. But since MyController doesn't have respondsToSelector either, so it crashes.

Error finding selector using NSTimer in Swift

Your class (it must be a class, not a struct or an enum) that this code belongs to must inherit from NSObject.

More strictly speaking whichever class you put in the target: parameter of NSTimer.scheduledTimerWithTimeInterval must inherit from NSObject. In this case, it happens to be self in another case, it may not be.

Got Unrecognized selector -replacementObjectForKeyedArchiver: crash when implementing NSCoding in Swift

Although Swift class works without inheritance, but in order to use NSCoding you must inherit from NSObject.

class Command: NSObject, NSCoding {
...
}

Too bad the compiler error is not very informative :(

NSTimer doesn't find selector

EDIT: Note that starting in Swift 2.2 you won't be able to make this mistake! You'll use the new #selector syntax (see https://stackoverflow.com/a/35658335/341994), and the compiler won't let you form a selector for a method that isn't exposed to Objective-C.


It's merely a question of exposing the Swift function to Objective-C so that it is visible to Objective-C. You have four choices:

  • Make TimerClass descend from NSObject (and delete the init implementation):

    class TimerClass : NSObject {
  • Declare TimerClass with @objc [not in Swift 2.0; use the previous choice instead]:

    @objc TimerClass {
  • Declare the function with @objc:

    @objc func timerEnd()
  • Declare the function dynamic (this is probably the worst choice, as it is unnecessary - the function is not dynamic; it does not need to be altered in place by Objective-C, it just needs to be visible):

    dynamic func timerEnd(){


Related Topics



Leave a reply



Submit