I'm getting this error with 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)
also I'm getting below error
'Name' is not a member type of 'Notification'
public let ImagePickerTrayDidHide: Notification.Name = Notification.Name(rawValue: "ch.laurinbrandner.ImagePickerTrayDidHide")
how I can fix that?
As I can guess initially you had the following code:
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)
When you compiled it in Xcode 10.1 you received the following errors: 'keyboardWillShowNotification' has been renamed to 'NSNotification.Name.UIKeyboardWillShow', Replace 'keyboardWillShowNotification' with 'NSNotification.Name.UIKeyboardWillShow'
and 'keyboardWillHideNotification' has been renamed to 'NSNotification.Name.UIKeyboardWillHide', Replace 'keyboardWillHideNotification' with 'NSNotification.Name.UIKeyboardWillHide'
.
Then you pressed "Fix" twice and get the incorrect code you already added to your question. You should use the following:
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)
Replace the NotificationCenter.Name.UIResponder
with UIResponder
For Eg:
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)
For More details refer https://stackoverflow.com/a/52325564/8331006
Replace UIResponder.NSNotification.Name.UIKeyboardWillShow
with .UIKeyboardWillShow
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.