Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NotificationCenter migrating swift 3 to swift 4.2 problem [duplicate]

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?

like image 561
CVar Avatar asked Sep 28 '18 03:09

CVar


1 Answers

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)

}
like image 136
OOPer Avatar answered Nov 10 '22 09:11

OOPer