专门针对可选型设定的,a为可选型变量
a ?? b // a != nil ? a! : b
可选型a如果不为nil,返回a的解包a!,否则,返回b,b的类型与a!相同
例子:var userName:String? //定义可选型变量 userName = "SmartTalk" if userName != nil { println("hello!,\(userName!)") } else{ println("Hello!,Guest") } //let outPutName:String = userName != nil ? userName! : "Guest" let outPutName:String = userName ?? "Guest" println("Hello!" + outPutName)
网友评论