Daily iOS
Swift

How optional chaining behaves

When you chain several optional accesses like this and hit nil partway through, what happens?

let city = user?.address?.city
How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

The chain stops the moment it hits nil from the left, skips the rest, and the whole thing becomes nil (short-circuit). The property accesses and method calls after that point never run.

Share a question