Python “or” equivalent in Swift?

前端 未结 2 1265
青春惊慌失措
青春惊慌失措 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)
    
    0 讨论(0)
  • 2021-01-19 05:28

    You could use the nil coalescing operator

    let variable = optional ?? default
    
    0 讨论(0)
提交回复
热议问题