Optional Chaining
Reach deep into nested optionals with the ?. operator. If any link is nil , the whole expression safely becomes nil instead of crashing — collapsing a pyramid of if let s into one clean line.
Learn Optional Chaining in our free Swift course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Swift course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
1️⃣ The Basics — a?.b
Put a ? after an optional, then a . to access something on it. If the optional is nil , the access is skipped and the result is nil . The result is always optional , so pair it with ?? to get a usable value.
2️⃣ Chaining Flattens, and ?. vs !
When the final property is itself optional, chaining doesn't pile up ?? — it flattens to a single optional. And unlike ! , which crashes on nil , ?. fails gracefully.
3️⃣ Methods and Subscripts
Chaining isn't just for properties. You can call methods ( obj?.method() ) and use subscripts ( obj?[i] , with the ? before the bracket). If the receiver is nil , the call simply doesn't happen.
Your turn. Fill in the chaining operator and the default operator.
📋 Quick Reference
No blanks this time — just a brief and an outline. Build the nested classes and reach the deepest value with a single chaining expression.
Practice quiz
What does optional chaining with ?. do when a link in the chain is nil?
- Crashes the program
- Returns a default value of 0
- Stops and returns nil for the whole expression
- Throws an error
Answer: Stops and returns nil for the whole expression. If any link is nil, the chain short-circuits and the whole expression evaluates to nil — no crash.
What is the type of the result of an optional chaining expression like a?.b where b is an Int?
- Int?
- Int
- Bool
- Optional<Optional<Int>>
Answer: Int?. Optional chaining always wraps the result in an optional, so accessing an Int property gives Int?.
If a property is already optional and you access it via chaining, does the result become doubly optional?
- Yes, it becomes Int??
- It becomes a non-optional
- It crashes
- No, optional chaining flattens it to a single level of optionality
Answer: No, optional chaining flattens it to a single level of optionality. Optional chaining flattens the result — you never get Int??; it stays a single Int?.
How does ?. differ from ! when accessing a property on an optional?
- They are identical
- ?. fails gracefully to nil; ! force-unwraps and crashes if nil
- ?. is faster
- ! returns nil
Answer: ?. fails gracefully to nil; ! force-unwraps and crashes if nil. ?. safely returns nil on a nil link, while ! crashes if the optional is nil.
Can you call a method through optional chaining, like person?.greet()?
- Yes — and the call simply doesn't happen if person is nil
- No, only properties
- Only on classes
- Only if greet returns nil
Answer: Yes — and the call simply doesn't happen if person is nil. Optional chaining works on method calls and subscripts too; the call is skipped when the receiver is nil.
What does the result of calling a Void-returning method via optional chaining (person?.printName()) become?
- Void
- Bool
- Void? — so you can check if it ran by comparing to nil
- Int?
Answer: Void? — so you can check if it ran by comparing to nil. Even a Void method returns Void? through chaining, letting you check 'if person?.printName() != nil' to see if it executed.
How do you write a subscript access through optional chaining?
The question mark goes before the subscript bracket: optionalArray?[0].
What is a clean way to provide a fallback when an optional chain returns nil?
- Use ! after the chain
- Use a for loop
- Use guard inside the chain
- Use the nil-coalescing operator ?? after the chain
Answer: Use the nil-coalescing operator ?? after the chain. Combine chaining with ??: person?.address?.city ?? "Unknown".
In a?.b?.c, if 'a' has a value but 'b' is nil, what happens to '.c'?
- It still evaluates
- It is skipped and the whole expression is nil
- It crashes
- It returns c's default
Answer: It is skipped and the whole expression is nil. The chain short-circuits at the first nil link, so .c is never reached and the result is nil.
Why is optional chaining preferred over nested if-let pyramids?
- It is the only way to unwrap
- It never returns nil
- It expresses a deep, possibly-nil access in one readable line
- It is required by the compiler
Answer: It expresses a deep, possibly-nil access in one readable line. Chaining collapses several nested unwraps into a single concise expression that reads left to right.