Capacity

Anonymous contributor's avatar
Anonymous contributor
Published Jun 11, 2025
Contribute to Docs

The capacity of a Swift array represents the amount of memory currently allocated to store elements without requiring a resize. If the number of elements exceeds this capacity, the array automatically reallocates memory, usually following an exponential growth pattern—to ensure efficient performance during appends.

Syntax

array.capacity

Parameters:

capacity is a read-only property and does not take any parameters.

Return value:

Returns the number of elements the array can store in its currently allocated memory without reallocating.

Example

The following code prints the array’s initial capacity, appends elements, and then prints the updated capacity to show how Swift manages memory allocation dynamically:

var numbers: [Int] = []
print(numbers.capacity)
numbers.append(1)
numbers.append(2)
numbers.append(3)
print(numbers.capacity)

The output of this code will be:

0
3

All contributors

Contribute to Docs

Learn Swift on Codecademy