Swift Array isEmpty

In this tutorial, we will learn about the Swift array isEmpty property with the help of examples.

The isEmpty property checks if the array is empty or not.


var languages = ["Swift", "C", "Java"]

// check if leanguages is empty or not var result = languages.isEmpty
print(result) // Output: false

isEmpty Syntax

The syntax of the array isEmpty property is:


Here, array is an object of the Array class.

isEmpty Return Values

The isEmpty property returns:

  • true - if the array does not contain any elements
  • false - if the array contains some elements

Example 1: Swift Array isEmpty

var names = ["Gregory", "Perry", "Nadal"]

// check if names is empty or not print(names.isEmpty)
var employees = [String]()
// check if employees is empty or not print(employees.isEmpty)



In the above example, since

  • names contain three string elements, the property returns false.
  • employees is an empty array, the property returns true.

Example 2: Using isEmpty With if...else

var names = [String]()

// true because names is an empty array if (names.isEmpty) {
print( "Array is empty") } else { print("Elements:", names) }


Array is empty

Here, the names array is empty, so the if block is executed.

Did you find this article helpful?