将代码转换为Swift 4.2时,通知名称错误 [英] Error with notification names while converting code to Swift 4.2
问题描述
下面的代码在Swift 4.2之前运行良好:
The code below was working fine before Swift 4.2:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
当我单击修复"选项时,它变为:
When I click the 'Fix' option, it becomes:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIResponder.keyboardWillShowNotification, object: nil)
但是它仍然被标记为错误.这是解释:
But it is still marked an error. Here is the explanation:
Type 'NSNotification.Name' has no member 'UIResponder'
然后我尝试删除'UIResponder':
And then I tried to delete 'UIResponder':
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.
...但是我不知道该如何完成.
...but I don't know how should I complete it.
推荐答案
正确的格式是:
UIResponder.keyboardWillShowNotification
...因此,您的代码变为:
...so, your code becomes:
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillChange(notification:)),
name: UIResponder.keyboardWillShowNotification,
object: nil
)
这是Xcode 10的一个已知问题.在更正通知名称时,自动修复功能无法在Swift 4.2上正常工作.
This is a known issue with Xcode 10. Automatic Fix-it is not working correctly for Swift 4.2 when it comes to correcting notification names.
在Swift 4.2中,许多Notification.Name
实例成为其他类中的实例变量.例如,keyboardWillShowNotification
现在是UIResponder
的实例变量.
In Swift 4.2, lots of Notification.Name
instances became instance variables in other classes. For example, keyboardWillShowNotification
is now an instance variable of UIResponder
.
这篇关于将代码转换为Swift 4.2时,通知名称错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!