3

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)

enter image description here

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?

Rakesha Shastri
  • 11,053
  • 3
  • 37
  • 50
Ali Hosseini
  • 31
  • 1
  • 2
  • This will help. https://stackoverflow.com/questions/52316676/type-nsnotification-name-has-no-member-keyboarddidshownotification/52325564#52325564 – Rakesha Shastri Apr 28 '19 at 08:09
  • @RakeshaShastri I can't understand because my code is a little bit different, can you explain for me? – Ali Hosseini Apr 28 '19 at 10:17
  • 1
    Your code is wrong. Not different. The right way to do it is mentioned in the link. – Rakesha Shastri Apr 28 '19 at 10:18
  • @RakeshaShastri but that's work on swift 3, can you fix this issue and post an answer? – Ali Hosseini Apr 28 '19 at 10:25
  • Where does it say Swift 3 there? It clearly says Swift 4.2. Did you even check the link? – Rakesha Shastri Apr 28 '19 at 11:30
  • 1
    Possible duplicate of [How to fix this issue with the UIResponder as not recognized?](https://stackoverflow.com/questions/55787125/how-to-fix-this-issue-with-the-uiresponder-as-not-recognized) and [Moving UITextField up with keyboard - changes in swift 4.2?](https://stackoverflow.com/questions/53082764/moving-uitextfield-up-with-keyboard-changes-in-swift-4-2) and [Error with notification names while converting code to Swift 4.2](https://stackoverflow.com/questions/52466147/error-with-notification-names-while-converting-code-to-swift-4-2) – rmaddy Apr 28 '19 at 16:06

3 Answers3

4

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)
Roman Podymov
  • 4,168
  • 4
  • 30
  • 57
4

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

BharathRao
  • 1,846
  • 1
  • 18
  • 28
0

Replace UIResponder.NSNotification.Name.UIKeyboardWillShow with .UIKeyboardWillShow

Creign
  • 134
  • 10