Тип «NSNotification.Name» не имеет члена «UIResponder» - PullRequest
1 голос
/ 28 апреля 2019

Я получаю эту ошибку с 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 ]

1 голос
/ 03 мая 2019

Как я могу догадаться, изначально у вас был следующий код:

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)
0 голосов
/ 17 июля 2019

Заменить 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

...