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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions content/swift/concepts/arrays/terms/capacity/capacity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
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:
- 'Swift'
- 'Arrays'
- 'Data Structures'
- 'Memory'
- 'Variables'
- 'Collections'
CatalogContent:
- 'learn-swift'
- 'paths/build-ios-apps-with-swiftui'
---

The ****capacity**** of a [Swift](https://www.codecademy.com/resources/docs/swift) [array](https://www.codecademy.com/resources/docs/swift/arrays) represents the amount of memory currently allocated to store elements without resizing. When the number of elements exceeds this capacity, the array automatically reallocates memory—typically using an exponential growth strategy—to maintain efficient appending performance.

## Syntax

var arrayName: [Int] = []
arrayName.capacity

## Example

var numbers: [Int] = []
print(numbers.capacity) // prints 0

numbers.append(1)
numbers.append(2)
numbers.append(3)
print(numbers.capacity) // prints 4