Does Swift have a null coalescing operator and if not, what is an example of a custom operator?

后端 未结 6 2234
醉酒成梦
醉酒成梦 2020-12-09 14:24

A common feature in many languages, the Null Coalescing Operator, is a binary operator often used to shorten expressions of the type:

x = possiblyNullValue N         


        
6条回答
  •  孤城傲影
    2020-12-09 15:24

    You can check with playground code below.

    import UIKit
    var personalSite : String?
    let defaultSite = "http://www.google.com"
    
    var website = personalSite ?? defaultSite
    

提交回复
热议问题