Capacity
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:
03
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Swift on Codecademy
- Skill path
Build iOS Apps with SwiftUI
Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.Includes 7 CoursesWith CertificateBeginner Friendly13 hours - Free course
Learn Swift
A powerful programming language developed by Apple for iOS, macOS, and more.Beginner Friendly12 hours