Python “or” equivalent in Swift?

前端 未结 2 1268
青春惊慌失措
青春惊慌失措 2021-01-19 04:58

Due to Swift cool behaviors I was looking for an or equivalent in Swift.

Something like this:

variable = value or default
2条回答
  •  无人共我
    2021-01-19 05:12

    Use ??:

    var optional:String?
    var defaultValue:String = "VALUE"
    
    var myString:String = optional ?? defaultValue
    
    print(myString)
    

提交回复
热议问题