Skip to content

[Term Entry] Swift Arrays: capacity #6969 #6991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 11, 2025
55 changes: 55 additions & 0 deletions content/swift/concepts/arrays/terms/capacity/capacity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
Title: 'Capacity'
Description: 'Returns the total number of elements that the array can contain without allocating new storage.'
Subjects:
- 'Computer Science'
- 'IOS'
- 'Mobile Development'
Tags:
- 'Arrays'
- 'Collections'
- 'Data Structures'
- 'Memory'
- 'Swift'
- 'Variables'
CatalogContent:
- 'learn-swift'
- 'paths/build-ios-apps-with-swiftui'
---

The **`capacity`** of a Swift [array](https://www.codecademy.com/resources/docs/swift/arrays) 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

```pseudo
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:

```swift
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:

```shell
0
3
```