Swift: Testing optionals for nil

前端 未结 14 643
攒了一身酷
攒了一身酷 2020-12-08 08:51

I\'m using Xcode 6 Beta 4. I have this weird situation where I cannot figure out how to appropriately test for optionals.

If I have an optional xyz, is the correct w

14条回答
  •  时光取名叫无心
    2020-12-08 09:28

    If you have conditional and would like to unwrap and compare, how about taking advantage of the short-circuit evaluation of compound boolean expression as in

    if xyz != nil && xyz! == "some non-nil value" {
    
    }
    

    Granted, this is not as readable as some of the other suggested posts, but gets the job done and somewhat succinct than the other suggested solutions.

提交回复
热议问题