-1

That's my code

let temperature = String(describing: Int(incomeTemp.text!))

celcjuszScore.text = "\(temperature)"
print(temperature)

Whent I am pushing a button, the result of print is "Optional(32)" (When I am writing 32 in incomeTemp). I would like to have "Optional" removed and only "32" should stay.

Hamish
  • 78,605
  • 19
  • 187
  • 280
Maciej Gieparda
  • 119
  • 3
  • 5
  • Why not just use the text as is, instead of converting it into an int and then immediately back into a string? – John Montgomery Sep 20 '17 at 16:19
  • Side note: don't use `String(describing: xyz)` to make a string from an int, just use `String(xyz)`. The "describing" is actually the same as `xyz.description` which does not always represent the string content as you would expect. – Eric Aya Sep 20 '17 at 16:56

2 Answers2

3

Just unwrap it.

if let temperature = Int(incomeTemp.text!) {
    celcjuszScore.text = "\(temperature)"
    print(temperature)
}
sCha
  • 1,454
  • 1
  • 12
  • 22
1

Remove the optional when converting text to number: Int(incomeTemp.text!) ?? 0.

Or solve the error explicitly:

if let temperature = Int(incomeTemp.text ?? "") {
   celcjuszScore.text = "\(temperature)"
} else {
   celcjuszScore.text = "Invalid temperature"
}
Sulthan
  • 128,090
  • 22
  • 218
  • 270