Swift inline conditional?
If you're looking for a one-liner to do that, you can pull the ?:
operation out of the string interpolation and concatenate with +
instead:
let fileExists = false // for example
println("something " + (fileExists ? "exists" : "does not exist"))
Outputs:
something does not exist
You can use the new Nil-Coalescing Operator, introduced in Swift 3. It will return default value if someOptional
is nil
.
let someValue = someOptional ?? ""
Here, if someOptional
is nil
, this operator will assign ""
to someValue
.