Nil Coalescing and Ternary with Optionals
Using ?? for default values and chaining nil-coalescing operators.
Welcome
The nil coalescing operator `??` provides a default value when an optional is nil. Combined with the ternary operator, it gives you concise, expressive optional handling.
The ?? Operator
```swift
let name: String? = nil
let display = name ?? "Guest"
print(display) // "Guest"
let known: String? = "Alice"
print(known ?? "Guest") // "Alice"
```
If the optional is non-nil, its unwrapped value is used. Otherwise the default is used.
All lessons in this course
- if let and guard let Binding
- Nil Coalescing and Ternary with Optionals
- Optional Chaining ?. Operator
- Implicitly Unwrapped Optionals and When to Avoid Them