This code works in the playground:
var x: Int? = nil let y = 5 x = x ?? y
and
var x: Int? = 6 let y = 5 x = x ?? y
By the way, some verification options for nil:
if x != nil { // classic } if x != .None { // nil in Swift is enum with value .None } if let _x = x { // if you need it value } if let _ = x { // if you don't need it value }
UPD: code for the project - copy and run it:
var x: Int? = nil let y = 5 x = x ?? y print (x) x = 7 x = x ?? y print (x)
source share