What is array.count in Swift?
Overview
The count property of an array in Swift gets the number of elements in the array.
Syntax
arr.count
Parameters
This does not take any parameters.
Return value
It returns an integer value which is the number of elements contained in the array arr.
Example
// create arraysvar gnaam = ["Google", "Netflix", "Amazon", "Apple", "Facebook", "Meta"];var letters = ["a", "b", "c", "d", "e"];var numbers = [1, 2, 3];// print number of elementsprint(gnaam.count);print(letters.count);print(numbers.count);
Explanation
- Lines 2β4: We create the arrays
gnaam,letters, andnumbers. - Lines 7β9: We print the number of elements each array contains using the
countproperty of the arrays.