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

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



With Swift 4.2, getting following error, that was working fine with Swift 4.



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



Here is my error code.


NotificationCenter.default.addObserver(forName: NSNotification.Name.UITextField.textDidChangeNotification, object: textField, queue: OperationQueue.main) (notification) in
loginAction.isEnabled = textField.text != ""



Full Code:


@IBAction func alertWithLogin()

let alertController = UIAlertController(title: "Please Enter Credential", message: nil, preferredStyle: .alert)

// ADD ACTIONS HANDLER
let loginAction = UIAlertAction(title: "Login", style: .default) (_) in

let loginTextField = alertController.textFields![0] as UITextField
let passwordTextField = alertController.textFields![1] as UITextField

// do something with after login

loginAction.isEnabled = false
alertController.addAction(loginAction)

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) (_) in
// do something

alertController.addAction(cancelAction)

// ADD TEXT FIELDS
alertController.addTextField (textField) in
textField.placeholder = "Email"

alertController.addTextField (textField) in
textField.placeholder = "Password"
textField.isSecureTextEntry = true

// enable login button when password is entered
NotificationCenter.default.addObserver(forName: NSNotification.Name.UITextField.textDidChangeNotification, object: textField, queue: OperationQueue.main) (notification) in
loginAction.isEnabled = textField.text != ""



// PRESENT
present(alertController, animated: true)



enter image description here




3 Answers
3



textDidChangeNotification is a member of UITextField (and UITextView).


textDidChangeNotification


UITextField


UITextView


NotificationCenter.default.addObserver(
self,
selector: #selector(self.keyboardDidShow(notification:)),
name: UITextField.textDidChangeNotification,
object: nil)






I was coming here next :D It seems like they moved all the notifications to their respective classes.

– Rakesha Shastri
Sep 14 '18 at 5:44







Worked for me in Swift 4.2 Thanks Buddy!!

– Ravi
Oct 3 '18 at 8:18


NotificationCenter.default.addObserver(forName: Notification.Name.UITextFieldTextDidChange, object: textField, queue: OperationQueue.main) (notification) in
//...



In swift sdk, All notification name should be the extension of struct: Notification.Name



So when use Notification.Name, you should ignore Class name(exc.UITextField), input "Notification.Name." then input some of your name(like "TextF") and use esc to show autocomplete






There is no such thing as Notification.Name.UITextFieldTextDidChange (at least as of Swift 4.2). Most notifications are not under Notification.Name (again, at least as of Swift 4.2).

– rmaddy
Jan 6 at 17:56



Notification.Name.UITextFieldTextDidChange


Notification.Name



When Migrating to 4.2 -



NSNotification.Name.UITextFieldTextDidEndEditing



NSNotification.Name.UITextFieldTextDidBeginEditing



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)