0Pricing
Swift Academy · Lesson

Optional Chaining ?. Operator

Calling methods and accessing properties on optionals without crashing.

Welcome

Optional chaining lets you call methods, access properties, and subscript through a chain of optionals — safely. If any link in the chain is nil, the whole expression returns nil without crashing.

The ?. Operator

```swift struct Address { var city: String } struct Person { var address: Address? } let alice = Person(address: Address(city: "Istanbul")) let bob = Person(address: nil) print(alice.address?.city) // Optional("Istanbul") print(bob.address?.city) // nil ```

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