halapgos1
halapgos1

Reputation: 1195

Issue printing UITextField Value

I have a UITextField on my storyboard and have an outlet to it in my controller.

@IBOutlet weak var textField: UITextField!

I've created an action (Editing Changed) for the UITextField. For now, I'm trying to print out the value as the value is changed but it is giving me the following error :

String interpolation produces a debug description of an optional value

Action Code :

@IBAction func editingChanged(_ sender: UITextField) {
    print("The value is : \(textField.text)")
}

Kind of confused why I'm having this issue.

Upvotes: 0

Views: 188

Answers (3)

vadian
vadian

Reputation: 285069

The text property of a text field is an optional, you have to unwrap the optional.

By the way you don't need the outlet, the sender parameter represents the text field

@IBAction func editingChanged(_ sender: UITextField) {
    print("The value is : \(sender.text!)")
}

You don't even need String Interpolation, you can also write

print("The value is : ", sender.text!)

Upvotes: 2

Yasin
Yasin

Reputation: 72

Maybe try:

@IBAction func editingChanged(_ sender: UITextField) {
    print("The value is : \(sender.text)")
}

The function should recognize your textfield as the sender.

Upvotes: 0

Mamdouh El-Nakeeb
Mamdouh El-Nakeeb

Reputation: 586

You should force unwrap the value of text field

print("The value is : \(textField.text!)")

Upvotes: 2

Related Questions