Can Swift optional be nested? -
i cannot find answer question, example can have optional of optional of string? tried write small test check out:
let : string? = nil; let b : string?? = a; if b!=nil { // error println("has value"); } else { println("fail"); }
but since not swift programmer don't know error saying "cannot assign result of expression".
yes can; syntax incorrect though. line:
if b!=nil
is digested compiler as:
if (b!) = nil
... thinks you're trying assign nil
unwrapped optional. swift doesn't allow make assignments within if
statements (in contrast objective-c). instead clearer:
if b != nil
edit: and, finish thought, proving syntactic sugar making optional optional, if add:
if let b = b { print("\(b)") }
you should see nil
printed output.
Comments
Post a Comment