iOS ● ○ ○ Swift 6

Day 1: Optional Chaining

Apr 6 2026
Test your knowledge of Swift’s optional chaining and nil coalescing operators.

What does this print?

let numbers: [Int]? = [1, 2, 3]
print(numbers?.count ?? 0)
print(numbers?[1] ?? 0)

let empty: [Int]? = nil
print(empty?.count ?? 0)


Try it in the online Swift Playground →

[spoiler title="Solution"]

Answer:

3
2
0

Explanation:

Optional chaining returns nil if the optional is nil, then nil coalescing provides the default value

[/spoiler]


Further Reading