NotificationCenter将Swift 3迁移到Swift 4.2问题

时间:2018-09-28 03:42:15

标签: swift syntax migration notificationcenter

我无法将我的代码从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

我整天都在编码,所以我什至看不到这段代码有什么问题。有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:11)

您使事情变得过于复杂了……

使用此代码,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)

}