繁体   English   中英

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

[英]Type 'NSNotification.Name' has no member 'UIResponder'

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)

在此处输入图像描述

我也遇到了错误

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

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

我该如何解决?

最初我可以猜到您有以下代码:

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'

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

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)

UIResponder例如,将NotificationCenter.Name.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

UIResponder.NSNotification.Name.UIKeyboardWillShow替换为.UIKeyboardWillShow

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM