9

The code below was working fine before Swift 4.2:

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)

When I click the 'Fix' option, it becomes:

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIResponder.keyboardWillShowNotification, object: nil)

But it is still marked an error. Here is the explanation:

Type 'NSNotification.Name' has no member 'UIResponder'

And then I tried to delete 'UIResponder':

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.

...but I don't know how should I complete it.

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
mannyCalavera
  • 593
  • 1
  • 4
  • 23

3 Answers3

33

The correct form is:

UIResponder.keyboardWillShowNotification

...so, your code becomes:

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

This is a known issue with Xcode 10. Automatic Fix-it is not working correctly for Swift 4.2 when it comes to correcting notification names.

In Swift 4.2, lots of Notification.Name instances became instance variables in other classes. For example, keyboardWillShowNotification is now an instance variable of UIResponder.

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
  • 1
    I didn't know this: ' Automatic Fix-it is not working correctly for Swift 4.2 when it comes to correcting notification names.' (Thanks a lot) – mannyCalavera Sep 23 '18 at 12:56
  • 2
    No luck for this code ....Implemented this code in UIWindow class "NotificationCenter.default.addObserver(self, selector: #selector(self.bringWindow(toTop:)), name: UIWindow.didBecomeVisibleNotification, object: nil)" – Raviteja Mathangi May 31 '19 at 13:23
  • Getting `Cannot invoke 'addObserver' with an argument list of type '(RegistrationViewController, selector: Selector, name: NSNotification.Name)'` – Sazzad Hissain Khan Mar 28 '20 at 16:46
  • 1
    @SazzadHissainKhan Sorry about that, Madhu_Nani was right - `object: nil` was missing. I've edited my post – Tamás Sengel Mar 28 '20 at 17:24
1

For someone else out there, I was building (what I thought was) a UI-Independent class and did not import UIKit.

Nothing worked until I added at the top of my file, this:

import UIKit

It appears some notifications (those in UIApplication, UIResponder etc..) may have been refactored into UIKIt.

Tunscopi
  • 97
  • 2
  • 8
0

The selected answer is incomplete and produce compilers error,

Cannot invoke 'addObserver' with an argument list of type '(RegistrationViewController, selector: Selector, name: NSNotification.Name)'

Here is the working format,

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256