Swift - Operators and Expressions
Identify the error in this Swift code using nil coalescing:
let x: Int? = nil let y: Int = 3 let z = x ?? y ?? 5 print(z)
let x: Int? = nil let y: Int = 3 let z = x ?? y ?? 5 print(z)
?? requires the right side to be the same type as the optional or a non-optional fallback value.x ?? y ?? 5x is Int?, y is Int, but y ?? 5 is invalid because y is not optional, so you cannot use ?? on it.15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions