-2

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
  • 74,809
  • 18
  • 177
  • 265
  • 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

2

Just unwrap it.

if let temperature = Int(incomeTemp.text!) {
    celcjuszScore.text = "\(temperature)"
    print(temperature)
}
sCha
  • 1,396
  • 1
  • 11
  • 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
  • 123,697
  • 21
  • 207
  • 260