Ana Ana - 1 year ago 131
Swift Question

How to convert Swift Bool? -> String?

Given a

, I'd like to be able to do this:

let a = BoolToString(optbool) ?? "<None>"

which would either give me
, or

Is there a built-in for

Answer Source
let b1: Bool? = true
let b2: Bool? = false
let b3: Bool? = nil

print(b1?.description ?? "none") // "true"
print(b2?.description ?? "none") // "false"
print(b3?.description ?? "none") // "none"

or you can define 'one liner' which works with both Bool and Bool? as a function

func BoolToString(b: Bool?)->String { return b?.description ?? "<None>"}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download