如何在Swift 4.2 中编写键盘通知?

How to write Keyboard notifications in Swift 4.2?

我正在尝试将此代码从 Swift 3 更新为 Swift 4.2

NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidShow), name: .UIKeyboardDidShow, object: nil);

到目前为止,我只是尝试了编译器提供的自动更正。这导致代码如下:

NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidShow), name: .UIResponder.keyboardDidShowNotification, object: nil);

不幸的是,这并没有带我走多远,导致了额外的错误: "Type of expression is ambiguous without more context"
请问有人解决了吗?

只需将 .UIResponder.keyboardDidShowNotification 替换为 UIResponder.keyboardDidShowNotification 即可解决您的问题。

最终代码为:

NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)