swift5 - 类型“NSNotification.Name”没有成员“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)
推荐阅读
- javascript - GridFS/multer - 无法读取未定义的属性“文件名”
- python - Pyspark - Fill.na 位置变化.when 条件结果
- dictionary - 一步有效地将h5py属性复制到python dict
- html - 如何在默认情况下模糊输入字段并在单击角度复选框后切换它
- php - 使用 PHP 从字符串中获取所有 HTML 标签?
- docker - 从 Docker 容器运行 Docker
- c# - 实体框架 - 如何手动清除连接池?SNIX_Execute 错误
- reactjs - 子集合 ID
- javascript - 检测链接并在本机反应中返回另一个视图
- tensorflow - tf。load_model 返回传递给优化器的意外关键字参数:名称