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 arrays
var gnaam = ["Google", "Netflix", "Amazon", "Apple", "Facebook", "Meta"];
var letters = ["a", "b", "c", "d", "e"];
var numbers = [1, 2, 3];
// print number of elements
print(gnaam.count);
print(letters.count);
print(numbers.count);

Explanation

  • Lines 2–4: We create the arrays gnaam, letters, and numbers.
  • Lines 7–9: We print the number of elements each array contains using the count property of the arrays.