NotificationCenter 将 swift 3 迁移到 swift 4.2 问题

NotificationCenter migrating swift 3 to swift 4.2 problem

我在尝试将我的代码从 Swift 3 迁移到 Swift 4.2

时遇到问题

这里是当前要迁移的代码:

fileprivate func observeKeyboardNotifications() {

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardShow), name: .UIKeyboardWillShow, object: nil)

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardHide), name: .UIKeyboardWillHide, object: nil)
}

这是我设法做到的:

fileprivate func observeKeyboardNotifications() {

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardShow), name: NSNotification.Name.UIResponder.UIKeyboardWillShowNotification, object: nil)

}

我仍然收到错误:

Type of expression is ambiguous without more context

我整天都在编码,所以我什至看不出这段代码有什么问题。谁能帮我解决这个问题?

你把事情搞得太复杂了……

使用此代码,Xcode 10 将向您显示正确的修复建议。

fileprivate func observeKeyboardNotifications() {

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardShow),
                                           name: UIKeyboardWillShowNotification, object: nil)

}

我的 Xcode 10 已将其修复为:

fileprivate func observeKeyboardNotifications() {

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardShow),
                                           name: UIResponder.keyboardWillShowNotification, object: nil)

}