optional array
check empty array
array validation
array handling
empty array check

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.

swift
1var values: [Int]? = nil
2
3if values?.isEmpty ?? true {
4    print("nil or empty")
5}

This works because:

  • 'values?.isEmpty is an optional Bool'
  • if values is nil, the expression becomes nil
  • '?? true treats that nil result as true'

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.

swift
1let values: [Int]? = []
2
3if let values {
4    if values.isEmpty {
5        print("array exists, but it is empty")
6    } else {
7        print("array has data")
8    }
9} else {
10    print("array is nil")
11}

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.

swift
1let values: [String]? = [ ]
2
3if values?.isEmpty == true {
4    print("present and empty")
5}

This version is slightly different from ?? true:

  • it returns true only when the array exists and is empty
  • it returns false when the array is nil

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.

swift
1func isNilOrEmpty<T>(_ array: [T]?) -> Bool {
2    array?.isEmpty ?? true
3}
4
5print(isNilOrEmpty(nil as [Int]?))
6print(isNilOrEmpty([]))
7print(isNilOrEmpty([1, 2, 3]))

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.

swift
1let values: [Int]? = nil
2
3if values == nil || values!.isEmpty {
4    print("avoid writing it this way")
5}

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 nil should count as empty.
  • Use array?.isEmpty ?? true when nil and empty should be treated the same.
  • Use array?.isEmpty == true when only an existing empty array should match.
  • Unwrap explicitly when nil and empty have different meanings in the app.
  • Avoid force unwrapping just to test isEmpty.

Course illustration
Course illustration

All Rights Reserved.