类 Y 的对象 X 没有在 Swift 中实现 methodSignatureForSelector

我有一个类 Person,它被实例化了多次。每个人都有自己的计时器。在我的 init对于 Person我调用 startTimer()

class Person {
var timer = NSTimer()
func startTimer() {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("timerTick"), userInfo: nil, repeats: true)
}


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

所以我可能在一个 Person[]数组中有3个 Person 实例。我得到一个错误:

2014-06-25 13:57:14.956 ThisProgram[3842:148856] *** NSForwarding: warning: object 0x113760048 of class '_TtC11ThisProgram6Person' does not implement methodSignatureForSelector: -- trouble ahead

我在其他地方读到,我应该从 NSObject继承,但这是在斯威夫特,而不是 Obj-C。这个函数在类中,所以我不知道该怎么做。

25484 次浏览

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()
}
}

Since XCode6 beta 6, you can use 'dynamic' func

dynamic func timerTick() { .... }

I had a similar error trying to use let encodedArchive = NSKeyedArchiver.archivedDataWithRootObject(archive) as NSData where archive was an array of a custom class. I found that declaring that custom class as a subclass of NSObject and NSCoding did the trick. It'll require a few more lines to conform to the protocol of NSCoding so it'll look something like this to start with:

class Person: NSObject, NSCoding {
init() {
super.init()
}


func encodeWithCoder(_aCoder: NSCoder) {   }
}