5

I am having trouble trying to migrate my piece of code from Swift 3 to Swift 4.2

here is the current code to be migrated:

fileprivate func observeKeyboardNotifications() {

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardShow), name: .UIKeyboardWillShow, object: nil)

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardHide), name: .UIKeyboardWillHide, object: nil)
}

Here's what I have managed to do :

fileprivate func observeKeyboardNotifications() {

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardShow), name: NSNotification.Name.UIResponder.UIKeyboardWillShowNotification, object: nil)

}

And still I get the error:

Type of expression is ambiguous without more context

I've been all day coding, so I can't really even see what's wrong with this code. Can anybody help me solve this?

CVar
  • 103
  • 1
  • 9

1 Answers1

14

You are making things too complicated than it should be...

With this code, Xcode 10 will show you the right fix-it suggestion.

fileprivate func observeKeyboardNotifications() {

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardShow),
                                           name: UIKeyboardWillShowNotification, object: nil)

}

My Xcode 10 has fixed it as:

fileprivate func observeKeyboardNotifications() {

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardShow),
                                           name: UIResponder.keyboardWillShowNotification, object: nil)

}
OOPer
  • 47,149
  • 6
  • 107
  • 142
  • Thanks for your answer!!! That must be the sign for my body saying me to call it a day , have been working on this all day – CVar Sep 28 '18 at 04:38
  • @CVar, thanks for reporting. Maybe you worked too hard and need some rest. Happy coding. – OOPer Sep 28 '18 at 04:40