arkilis
6/2/2017 - 11:59 PM

swift_optional2.swift

swift_optional2.swift


func getPrice(name: String) -> Float? {
    if (name == "item1") {
        return 10.0
    } else if (name == "item2") {
        return 20.0
    }
    return nil
}


var price:Float? = getPrice(name: "item")
let text = "Price is - "
let message = text + "\(price!)"  // fatal error: unexpectedly found nil while unwrapping an Optional value
print(message)