Optional chaining
Also called: Safe call
Reading a value through a chain of properties that may be missing, getting nothing back instead of a crash.
Definition
Optional chaining uses ?. to step through properties or method calls that may be empty. If any link in the chain is missing, the whole expression evaluates to nil (Swift) or null (Kotlin) instead of crashing. It is usually combined with a default value: ?? in Swift, the Elvis operator ?: in Kotlin.
In practice
order?.customer?.address?.city ?? "Unknown" shows "Unknown" when the order has no customer or the customer has no address.
Learn it in context
Lessons that use this term.