NotificationCenter migrating swift 3 to swift 4.2 problem [duplicate]
Asked Answered
L

1

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?

Lynching answered 28/9, 2018 at 3:42 Comment(0)
T
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)

}
Tearoom answered 28/9, 2018 at 3:49 Comment(2)
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 dayLynching
@CVar, thanks for reporting. Maybe you worked too hard and need some rest. Happy coding.Tearoom

© 2022 - 2024 — McMap. All rights reserved.