Search code examples
swiftlabeldoubleoption-typeunwrap

Showing optional double number on a label


I'm trying to show a double number on a label. But it shows as an optional. I can't unwrap it using "!" because some of the data is nil.

How can I make the "Optional(...)" text not show up?

This is how it looks:

let row = elementArray[indexPath.row]
mPoint.text = String(row.meltPoint) //Optional(2070.0)

Solution

  • Use if let to unwrap optional values:

    let row = elementArray[indexPath.row]
    if let myNumber = row.meltPoint {
        mPoint.text = "\(myNumber)"
    } else {
         mPoint.text = "N/A"
    }