如何将 NSNotification 作为参数传递给 Swift 中的选择器函数

how to pass an NSNotification as parameter to the selector function in Swift

我正在尝试实现一个 UIKeyboardWillShowNotification 来处理键盘出现时我的视图位置。 我添加我的观察者:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow", name: UIKeyboardWillShowNotification, object: nil)

然后我有keyboardWillShow函数:

func keyboardWillShow(notification: NSNotification){
    //Need to access to the notification here
}

在函数 keyboardWillShow 中,我需要接收 NSNotification 才能访问用户信息,但出现此错误:

"unrecognized selector sent to instance 0x7fd993e7d020"

你忘了冒号 :。将其更改为:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)