← All examples

Language • Beginner

Handling Optionals

Unwrap optionals safely with if-let, guard-let, and nil coalescing.

  • Optionals
  • Basics
  • Safety

Handling Optionals

optionals.swift

Checking for a value

Early exit with guard

Providing a default

Executing the code above will output:

swift run optionals.swift

Found value 42
No name provided
Hello, Chris!
Using fallback: 42