Swift – Array Size

Welcome to Swift Tutorial. In this tutorial, we will learn how to get the size of an array in Swift.

To get the size of an array in Swift, use count function on the array.

Following is a quick example to get the count of elements present in the array.

array_name.count

array_name is the array variable name.

count returns an integer value for the size of this array.

Example 1 – Get the size of an Array in Swift using count function

In the following example, we have two arrays. First array values is empty. Second array numbers is an integer array with three elements.

main.swift

var values:[Int] = []
print( "size of values is : \(values.count)" )

var numbers:[Int] = [7, 54, 21]
print( "size of numbers is : \(numbers.count)" )

Output

size of values is : 0
size of numbers is : 3
ADVERTISEMENT

Conclusion

In this Swift Tutorial, we have learned to get the size of the array or count the number of elements in the array using Swift example programs.