Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

count
array
swift
communitycreator

What is array.count in Swift?

Theodore Kelechukwu Onyejiaku

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);
Print number of elements of an array in Swift

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.

RELATED TAGS

count
array
swift
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku
RELATED COURSES

View all Courses

Keep Exploring