Error: Type 'UIResponder' has no member 'NSNotification'

After updating OS and Xcode I get errors from external librarys. The initial error is:


'keyboardWillShowNotification' has been renamed to 'NSNotification.Name.UIKeyboardWillShow' - Replace 'keyboardWillShowNotification' with 'NSNotification.Name.UIKeyboardWillShow'

If I do what Xcode wants, I get a new error:


Type 'UIResponder' has no member 'NSNotification'

How can I resolve this?

I call the following in my code, with XCode 11.1 :


NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)


and get no error nor warning.


But calling:

NotificationCenter.default.removeObserver(self, name: NSNotification.Name.keyboardWillShowNotification, object: nil)

yields error:

Type NSNotification.Name has no member keyboardWillShowNotification


and


NotificationCenter.default.removeObserver(self, name: UIResponder.NSNotification.Name.keyboardWillShowNotification, object: nil)

yields error:

Type UIResponder has no member NSNotification


So, simply use UIResponder.keyboardWillShowNotification


Could you show the code where you get the error ?


When I command-click on to jump to Definition, I get to UIKit / UIWindow:


extension UIResponder {
   
    public class let keyboardWillShowNotification: NSNotification.Name

    public class let keyboardDidShowNotification: NSNotification.Name

    public class let keyboardWillHideNotification: NSNotification.Name

    public class let keyboardDidHideNotification: NSNotification.Name

Here is the code that yields error:


overrideopenfunc viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        NotificationCenter.default.addObserver(self, selector: #selector(SCLAlertView.keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification.Name.UIKeyboardWillShow, object: nil); // Error: Value of type 'NSNotification.Name' has no member 'Name'
        NotificationCenter.default.addObserver(self, selector: #selector(SCLAlertView.keyboardWillHide(_:)), name:UIResponder.NSNotification.Name.UIKeyboardWillHide, object: nil); // Error: Value of type 'NSNotification.Name' has no member 'Name'
    }
   
    open override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil) //Error: 'keyboardWillShowNotification' has been renamed to 'NSNotification.Name.UIKeyboardWillShow'
        NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil) //Error: 'keyboardWillShowNotification' has been renamed to 'NSNotification.Name.UIKeyboardWillShow'
    }

Could there be a bug in my Xcode? I have tried to delete and reinstall Xcode, but still the same errors.

There are some typos in this code (overrideopenfunc vs override open func).


However, could you show how you defined

SCLAlertView


How did you declare

keyboardWillShow

Did you declare it as @objc ? You should.


You should change the way you call:

        NotificationCenter.default.addObserver(self, selector: #selector(SCLAlertView.keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification.Name.UIKeyboardWillShow, object: nil); // Error: Value of type 'NSNotification.Name' has no member 'Name'

to

        NotificationCenter.default.addObserver(self, selector: #selector(SCLAlertView.keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil);

syntax : UIResponder.keyboardWillShowNotification.Name.UIKeyboardWillShow. is incorrect


I do not understand the problem for:

        NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil) //Error: 'keyboardWillShowNotification' has been renamed to 'NSNotification.Name.UIKeyboardWillShow'

It work for me with XCode 11.1 (which XCode version are you using ?).


Could you do a command-click on keyboardWillShowNotification and select jump to definition.

Report what you get.

Error: Type 'UIResponder' has no member 'NSNotification'
 
 
Q