.isEmpty

Anonymous contributor's avatar
Anonymous contributor
Published Sep 25, 2023
Contribute to Docs

The .isEmpty property returns true if an array contains no elements, and returns false if the array contains one or more elements.

Syntax

arrayName.isEmpty

Where arrayName is the array being checked for elements.

Example

The following example creates and array and evaluates the contents of the array with .isEmpty before and after adding an element:

var favoriteSongs = [String]()
print(favoriteSongs.isEmpty)
favoriteSongs.append("Stayin' Alive")
print(favoriteSongs.isEmpty)

This will output:

true
false

Note: Big-O of this method is O(1).

All contributors

Contribute to Docs

Learn Swift on Codecademy