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.
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources