Swift – Check if an Array is Empty

Use the isEmpty property to check whether a Swift array contains no elements. It returns true when the array has zero elements and false when the array contains one or more elements.

The check works with arrays of any element type, including [Int], [String], custom objects, and nested arrays.

Swift Array isEmpty Syntax

Read the isEmpty property from the array.

</>
Copy
array_name.isEmpty

isEmpty is a Boolean property, not a function, so it is written without parentheses. Use it directly in an if condition or store its result in a Bool variable.

</>
Copy
if arrayName.isEmpty {
    // The array has no elements.
}

Example 1 – Check if Array is Empty in Swift

In this Swift example program, we will demonstrate isEmpty function on an empty array and a non-empty array.

main.swift

</>
Copy
var values:[Int] = []

var numbers:[Int] = [7, 54, 21]

print( "values is empty? \(values.isEmpty)" )
print( "numbers is empty? \(numbers.isEmpty)" )

Output

values is empty? true
numbers is empty? false

The values array has no elements, so values.isEmpty is true. The numbers array contains three integers, so numbers.isEmpty is false.

Use isEmpty in an if-else Statement

An if-else statement is useful when the program should perform different actions for empty and non-empty arrays.

</>
Copy
let names = ["Asha", "Ravi"]

if names.isEmpty {
    print("No names are available.")
} else {
    print("The array contains \(names.count) names.")
}

Output

The array contains 2 names.

Check if a Swift Array Is Not Empty

Prefix isEmpty with the logical NOT operator ! when code should run only if the array contains at least one element.

</>
Copy
let tasks = ["Review code", "Run tests"]

if !tasks.isEmpty {
    print("Next task: \(tasks[0])")
}

Checking !array.isEmpty before accessing the first element prevents an out-of-range access for an empty array. When only the first element is required, optional binding with array.first is another safe option.

</>
Copy
if let firstTask = tasks.first {
    print("Next task: \(firstTask)")
}

isEmpty Compared with count == 0

Both of the following expressions return the same result for a Swift array:

</>
Copy
array.isEmpty
array.count == 0

Prefer isEmpty when the intent is to test whether the array has no elements. It states the purpose directly and also works consistently with other Swift collection types.

Use count when the actual number of elements is needed, such as checking whether an array contains at least three values.

</>
Copy
let scores = [82, 91, 76]

if scores.count >= 3 {
    print("At least three scores are available.")
}

Check an Optional Swift Array for nil or Empty

An optional array can be nil, an empty array, or a non-empty array. Decide whether nil should be treated as empty before choosing the expression.

The following expression treats both nil and an empty array as empty:

</>
Copy
var items: [String]? = nil

if items?.isEmpty ?? true {
    print("The array is nil or empty.")
}

Optional chaining makes items?.isEmpty return Bool?. The nil-coalescing operator supplies true when items is nil.

When nil and an empty array must be handled separately, unwrap the optional first.

</>
Copy
var items: [String]? = []

if let items {
    if items.isEmpty {
        print("The array exists but is empty.")
    } else {
        print("The array contains elements.")
    }
} else {
    print("The array is nil.")
}

Check String, Object, and Nested Arrays with isEmpty

The element type does not change how isEmpty works.

</>
Copy
struct User {
    let name: String
}

let words: [String] = []
let users: [User] = []
let groups: [[Int]] = [[], [10, 20]]

print(words.isEmpty)
print(users.isEmpty)
print(groups.isEmpty)
print(groups[0].isEmpty)

In the nested-array example, groups.isEmpty is false because the outer array contains two inner arrays. However, groups[0].isEmpty is true because the first inner array contains no integers.

Check Array Emptiness After Filtering or Removing Elements

An array can become empty after a transformation or mutation. Check the resulting array rather than assuming that it still contains values.

</>
Copy
let numbers = [1, 3, 5]
let evenNumbers = numbers.filter { $0.isMultiple(of: 2) }

if evenNumbers.isEmpty {
    print("No even numbers were found.")
}

Output

No even numbers were found.
</>
Copy
var queue = ["A", "B"]
queue.removeAll()

print(queue.isEmpty)

Output

true

Reusable Empty Check for Swift Collections

When a helper should accept arrays and other collection types, define it with a generic Collection constraint and read the collection’s isEmpty property.

</>
Copy
func printEmptyStatus<C: Collection>(_ collection: C) {
    print(collection.isEmpty ? "Empty" : "Not empty")
}

printEmptyStatus([Int]())
printEmptyStatus(["Swift"])

Output

Empty
Not empty

Common Mistakes When Checking Swift Arrays

  • Calling isEmpty as a method: write array.isEmpty, not array.isEmpty().
  • Forcing an optional array: avoid optionalArray!.isEmpty unless the value is guaranteed to be non-nil.
  • Accessing index 0 before checking: an empty array has no valid element index.
  • Confusing an empty outer array with empty inner arrays: check the specific nesting level that matters.
  • Using count when only emptiness matters: isEmpty communicates the intent more clearly.

Swift Array isEmpty FAQs

What does isEmpty return for a Swift array?

It returns true when the array contains zero elements and false when it contains one or more elements.

Is isEmpty a property or a function in Swift?

isEmpty is a read-only Boolean property. Use array.isEmpty without parentheses.

How do I check whether a Swift array is not empty?

Use !array.isEmpty. The condition is true when the array contains at least one element.

Should I use isEmpty or count == 0 in Swift?

Use isEmpty when you only need to know whether the array has no elements. Use count when you need the number of elements or a numerical comparison.

How do I check whether an optional array is nil or empty?

Use optionalArray?.isEmpty ?? true when both nil and an empty array should be treated as empty. Unwrap the optional first when those states need different handling.

Swift Array Empty-Check Editorial QA Checklist

  • Confirm the tutorial describes isEmpty as a property rather than a function.
  • Verify examples show both empty and non-empty arrays with the expected Boolean results.
  • Check that optional-array examples state whether nil is treated as empty or handled separately.
  • Ensure no example accesses an array element before confirming that a valid element exists.
  • Confirm all new Swift, syntax-only, and output blocks use the required PrismJS classes.

Swift Array isEmpty Summary

In this Swift Tutorial, you learned to use array.isEmpty for empty and non-empty arrays, negate it for non-empty checks, compare it with count == 0, and handle optional and nested arrays safely.