KawaiKx
KawaiKx

Reputation: 9940

How a textfield value can be used as a double in swift 4?

We have an outlet in swift 4 like this:

@IBOutlet var accured_sum: UITextField!

I declared another variable like this:

var sum: Double?

Now I want to assign Double value of accured_sum to variable sum.

How do I do that?

Upvotes: 1

Views: 805

Answers (2)

Sergey Grishchev
Sergey Grishchev

Reputation: 12051

You can try something like

guard let text = accured_sum.text else { return }
sum = Double(text)

Upvotes: 2

J. Doe
J. Doe

Reputation: 13113

This is another solution, providing a default value

sum = Double(TextField.text ?? "0")

If sum is not nil, it has the double from the TextField. It is is nil: your textfield was not a double OR you never initialized sum.

Upvotes: 0

Related Questions