我已经使用以下代码成功设置了通知观察者:
func setupNotification {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "action:", name: notificationString, object: nil)
}
func action(notification: NSNotification) {
// Do something when receiving notification
}
但是,我对上面的编码风格不感兴趣,因为我可能会输入或复制/粘贴错误的方法名称action:
。
所以我尝试以addObserver
不同的方式:NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector(/* What to pass in here??? */), name: notificationString, object: nil)
,我不知道要传递什么selector: Selector(...)
。
XCode 提示我:Selector(action(notification: NSNotification)
,但这是非法的。
在 Objective C 中,我可以在这个阶段轻松地选择一个方法,但我不知道如何在 Swift 中。
你试过这种语法吗?让我知道。
谢谢,