Check if optional array is empty
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When an array is optional, there are really two different states you may care about: the array might be nil, or it might exist but contain no elements. Good code makes that distinction explicit instead of treating both cases accidentally. In Swift, the cleanest solution depends on whether you want nil to count as "empty" or whether you want to handle nil and [] differently.
The Most Common Swift Check
If you want nil to behave the same as an empty array, use the nil-coalescing operator with isEmpty.
This works because:
- '
values?.isEmptyis an optionalBool' - if
valuesisnil, the expression becomesnil - '
?? truetreats thatnilresult astrue'
The same code also works when the array exists but has zero elements.
Distinguish nil From Empty
Sometimes nil and empty mean different things. For example, nil might mean "not loaded yet" while [] means "loaded and there were no results."
In that case, unwrap the optional first.
This is more verbose, but it is the right choice when the two states carry different business meaning.
A Compact Comparison Pattern
Another short pattern you may see is comparing directly against true.
This version is slightly different from ?? true:
- it returns
trueonly when the array exists and is empty - it returns
falsewhen the array isnil
That makes it useful when nil should not count as empty.
Wrap The Logic In A Helper When Reused
If the same check appears everywhere, a helper can make the calling code easier to read.
This is especially nice in view models or validation code where you want the intent to be obvious at a glance.
Why Optional Chaining Helps
Without optional chaining, you end up writing manual checks that are longer and easier to get wrong.
That code works only because of short-circuit evaluation, but it is less safe and less readable. Force unwrapping inside a condition is rarely the best answer when Swift already gives you optional chaining.
Common Pitfalls
The most common mistake is forgetting to decide whether nil should count as empty. values?.isEmpty == true and values?.isEmpty ?? true answer different questions.
Another issue is force unwrapping an optional array just to call isEmpty. That creates avoidable crash risk when the array is nil.
Developers also sometimes write very dense one-liners when the business meaning is subtle. If nil and empty represent different states in the app, write that logic explicitly.
Finally, keep the code consistent across the project. Optional array checks become harder to reason about when different files silently treat nil differently.
Summary
- Decide first whether
nilshould count as empty. - Use
array?.isEmpty ?? truewhenniland empty should be treated the same. - Use
array?.isEmpty == truewhen only an existing empty array should match. - Unwrap explicitly when
niland empty have different meanings in the app. - Avoid force unwrapping just to test
isEmpty.

