0Pricing
Swift Academy · Lesson

Optional Chaining ?. Operator

Calling methods and accessing properties on optionals without crashing.

Optional Chaining ?. Operator is a free Swift Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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 ```

Chaining Multiple Levels

Chains can be arbitrarily deep: ```swift struct Company { var ceo: Person? } let company = Company(ceo: alice) let city = company.ceo?.address?.city print(city ?? "Unknown") // "Istanbul" ``` If `ceo` or `address` is nil, `city` becomes nil.

Calling Methods via Chaining

Methods can also be called through optional chaining: ```swift var name: String? = "alice" let upper = name?.uppercased() // Optional("ALICE") name = nil let upper2 = name?.uppercased() // nil ``` The return type becomes optional: `String` becomes `String?`.

Subscripts via Chaining

```swift var dict: [String: [Int]]? = ["scores": [10, 20, 30]] let first = dict?["scores"]?[0] // Optional(10) dict = nil let none = dict?["scores"]?[0] // nil ``` Subscripts work the same as properties and methods in a chain.

Chaining Produces an Optional

Even if the final result is non-optional, chaining wraps it in Optional: ```swift struct Cat { var lives: Int = 9 } var cat: Cat? = Cat() let lives = cat?.lives // Int? not Int ``` Unwrap with `??`, `if let`, or `guard let` as needed.

Setting Values Through Chaining

You can set properties through optional chains: ```swift var profile: UserProfile? = UserProfile() profile?.name = "Alice" ``` If `profile` is nil, the assignment is silently ignored (no crash).

Calling Void Methods via Chaining

```swift var manager: SoundManager? manager?.play() // silently ignored if nil ``` The call returns `Void?` — `()` if non-nil, `nil` if the optional was nil. You can check the result to know if the call happened.

Optional Chaining vs Forced Unwrap

```swift // Forced unwrap — crashes if nil: let city = alice.address!.city // Optional chaining — safe: let city = alice.address?.city ``` Always prefer optional chaining. Use `!` only when you're certain nil cannot occur.

Combining with ?? for Defaults

Chain `??` after optional chaining for clean defaults: ```swift let displayCity = user.profile?.address?.city ?? "Unknown City" let count = database?.records?.count ?? 0 ``` The entire chain either resolves to a value or falls back to the default.

Quick Check

If any link in an optional chain is `nil`, what does the whole expression return?

Recap

Key takeaways: • `?.` safely accesses properties, calls methods, and subscripts optionals • If any link is nil, the whole chain returns nil • The result type is always optional (even if the final value normally isn't) • Combine `?.` with `??` for clean safe-access + default patterns • Prefer `?.` over `!` to avoid crashes Next: implicitly unwrapped optionals.

Frequently asked questions

Is the “Optional Chaining ?. Operator” lesson free?

Yes — the full text of “Optional Chaining ?. Operator” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Optional Chaining ?. Operator”?

Calling methods and accessing properties on optionals without crashing. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Optional Chaining ?. Operator” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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