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

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -