类型“ NSNotification.Name”没有成员“ UIResponder”

时间:2019-04-28 06:53:29

标签: swift5

我在Swift 5中遇到此错误

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: NSNotification.Name.UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: NSNotification.Name.UIResponder.keyboardWillHideNotification, object: nil)

enter image description here

我也遇到错误

“名称”不是“通知”的成员类型

public let ImagePickerTrayDidHide: Notification.Name = Notification.Name(rawValue: "ch.laurinbrandner.ImagePickerTrayDidHide")

我该如何解决?

2 个答案:

答案 0 :(得分:1)

据我最初猜测,您有以下代码:

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)

在Xcode 10.1中进行编译时,收到以下错误:'keyboardWillShowNotification' has been renamed to 'NSNotification.Name.UIKeyboardWillShow', Replace 'keyboardWillShowNotification' with 'NSNotification.Name.UIKeyboardWillShow''keyboardWillHideNotification' has been renamed to 'NSNotification.Name.UIKeyboardWillHide', Replace 'keyboardWillHideNotification' with 'NSNotification.Name.UIKeyboardWillHide'

然后您按两次“ Fix”(确定),并获得了已经添加到问题中的错误代码。您应该使用以下内容:

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

答案 1 :(得分:0)

NotificationCenter.Name.UIResponder替换为UIResponder 例如:

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

NotificationCenter.default.addObserver(
self, 
selector: #selector(self.keyboardWillHide(_:)), 
name: UIResponder.keyboardWillHideNotification, object: nil)

有关更多详细信息,请参见https://stackoverflow.com/a/52325564/8331006