Swift: Testing optionals for nil

前端 未结 14 683
攒了一身酷
攒了一身酷 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:16

    In Xcode Beta 5, they no longer let you do:

    var xyz : NSString?
    
    if xyz {
      // Do something using `xyz`.
    }
    

    This produces an error:

    does not conform to protocol 'BooleanType.Protocol'

    You have to use one of these forms:

    if xyz != nil {
       // Do something using `xyz`.
    }
    
    if let xy = xyz {
       // Do something using `xy`.
    }
    

提交回复
热议问题