0
0
Swiftprogramming~8 mins

Optional chaining with ?. in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Optional chaining with ?.
Start with Optional Variable
Use Optional Chaining ? to access property/method
Is Optional nil?
YesReturn nil, stop
No
Access property/method safely
Return Optional result
Optional chaining tries to access a property or method on an optional value. If the optional is nil, it stops and returns nil safely.
Execution Sample
Swift
class Person {
  var pet: Pet?
}
class Pet {
  var name: String
  init(name: String) { self.name = name }
}

let person = Person()
let petName = person.pet?.name
This code tries to get the pet's name from a person who may or may not have a pet, using optional chaining.
Execution Table
StepExpressionOptional ValueActionResult
1person.petnilCheck if pet existsnil (pet is nil)
2person.pet?.namenilOptional chaining stops, returns nilnil
💡 person.pet is nil, so optional chaining returns nil without error
Variable Tracker
VariableStartAfter 1Final
person.petnilnilnil
petNameundefinednilnil
Key Moments - 2 Insights
Why does person.pet?.name return nil instead of causing an error?
Because person.pet is nil, optional chaining safely stops and returns nil instead of trying to access name on a nil value (see execution_table step 2).
What happens if person.pet is not nil?
Optional chaining accesses the pet's name normally and returns it wrapped as an optional (not shown in this trace but would be a non-nil value).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of person.pet at step 1?
Anil
BA Pet object
CAn error
DUndefined
💡 Hint
Check the 'Optional Value' column in execution_table row 1
At which step does optional chaining stop because the optional is nil?
AStep 1
BStep 2
CIt never stops
DAfter step 2
💡 Hint
Look at the 'Action' column in execution_table row 2
If person.pet was not nil, what would person.pet?.name return?
Anil
BAn error
CThe pet's name wrapped in an optional
DUndefined
💡 Hint
Recall that optional chaining returns nil only if the optional is nil, otherwise it returns the accessed value as optional
Concept Snapshot
Optional chaining with ? allows safe access to properties or methods on optionals.
If the optional is nil, the whole expression returns nil without error.
Syntax: optionalValue?.property or optionalValue?.method()
Useful to avoid crashes when optionals might be nil.
Returns an optional result.
Full Transcript
This visual trace shows how optional chaining works in Swift. We start with an optional property person.pet which is nil. When we try to access person.pet?.name, optional chaining checks if pet is nil. Since it is nil, it stops and returns nil safely without causing an error. This prevents crashes from trying to access properties on nil. If pet was not nil, optional chaining would access the name property and return it wrapped as an optional. The variable tracker shows person.pet remains nil and petName becomes nil after the optional chaining expression. Key moments clarify why optional chaining stops safely and what happens if the optional is not nil. The quiz tests understanding of these steps and results.