Type 'NSNotification.Name' has no member &

2019-01-19 21:08发布

I'm getting this error with Swift 4.2

Type 'NSNotification.Name' has no member 'keyboardDidShowNotification'

Here is my code:

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidShow(notification:)), name: NSNotification.Name.keyboardDidShowNotification, object: nil)

Following one was working fine with Swift 4 but not with Swift 4.2

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidShow(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)

enter image description here

Apple document Ref: NSNotification.Name.keyboardDidShowNotification

2条回答
Emotional °昔
2楼-- · 2019-01-19 21:40

Working on swift 4,2

 func bindToKeyboard(){
    NotificationCenter.default.addObserver(self, selector: #selector(UIView.keyboardWillChange(_:)), name:
        UIApplication.keyboardWillChangeFrameNotification
        , object: nil)


}
查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-19 21:43

I believe you use it like this now.

NotificationCenter.default.addObserver(
    self, 
    selector: #selector(self.keyboardDidShow(notification:)), 
    name: UIResponder.keyboardDidShowNotification, object: nil) //You can use any subclass of UIResponder too

It is listed in UIResponder doc as a Type Property.

查看更多
登录 后发表回答