Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

capacity
swift
communitycreator

What is array.capacity in Swift?

Theodore Kelechukwu Onyejiaku

Overview

In Swift, capacity is a property of an array. It gets the total number of elements that the array can contain without allocating new storage.

Syntax

arr.capacity

Parameters

This method takes no parameter.

Return Value

It returns an integer value.

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.capacity);
print(letters.capacity);
print(numbers.capacity);
Using the array.capacity property

Explanation

  • Lines 2–4: We create the arrays gnaam, letters, and numbers.
  • Line 7–9: We print the number of elements each array can contain without allocating new storage using the capacity property of arrays.

RELATED TAGS

capacity
swift
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku
RELATED COURSES

View all Courses

Keep Exploring