0Pricing
Swift Academy · Lesson

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

  1. if let and guard let Binding
  2. Nil Coalescing and Ternary with Optionals
  3. Optional Chaining ?. Operator
  4. Implicitly Unwrapped Optionals and When to Avoid Them
← Back to Swift Academy