NSNotification无法找到选择器

时间:2015-03-01 19:45:34

标签: ios cocoa-touch swift unrecognized-selector nsnotification

我通过默认中心发布通知,如下所示:

NSNotificationCenter.defaultCenter().postNotificationName(ColorDidGetTappedNotification, object: self)

我在另一个例子中观察通知,如下所示:

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("colorDidGetTapped:"), name: ColorDidGetTappedNotification, object: nil)
....
func colorDidGetTapped(notification: NSNotification) {
    println("Notification recieved")
}

但是我得到一个无法识别的选择器异常:

更新1

*** NSForwarding: warning: object 0x7d564490 of class '_D.GameModel' does not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector -[_D.GameModel colorDidGetTapped:]

2 个答案:

答案 0 :(得分:5)

检查GameModel是NSObject的子类

class GameModel: NSObject {

}

答案 1 :(得分:0)

尝试使用此方法观察通知:addObserverForName(_:object:queue:usingBlock:)。而不是选择器传递块