Swift – Check if an Array is Empty

Welcome to Swift Tutorial. In this tutorial, we will learn how to check if an array is empty in Swift programming.

To check if an array is empty, use Swift function isEmpty on the array.

Following is a quick example, to check if an array is empty.

array_name.isEmpty

The function returns a boolean value. If the array is empty, isEmpty returns true, else false.

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

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
ADVERTISEMENT

Conclusion

In this Swift Tutorial, we have learned to check if an array is empty or not with the help of example Swift programs.