Swift – Array Size

Use the count property to get the number of elements in a Swift array. The result is an Int. An empty array has a count of 0.

Swift uses count rather than length, size, length(), or size() for arrays.

Following is a quick example to get the count of elements present in the array.

</>
Copy
array_name.count

array_name is the array variable name.

count returns an integer value for the size of this array.

Example 1 – Get the size of an Array in Swift using count function

In the following example, we have two arrays. First array values is empty. Second array numbers is an integer array with three elements.

main.swift

</>
Copy
var values:[Int] = []
print( "size of values is : \(values.count)" )

var numbers:[Int] = [7, 54, 21]
print( "size of numbers is : \(numbers.count)" )

Output

size of values is : 0
size of numbers is : 3

The empty array reports a size of 0, while the second array reports 3 because it contains three integers.

Check Whether a Swift Array Is Empty

Use isEmpty when the requirement is only to check whether an array contains any elements. This expresses the intent more directly than comparing count with zero.

</>
Copy
let names: [String] = []

if names.isEmpty {
    print("The array is empty.")
} else {
    print("The array contains \(names.count) elements.")
}

Output

The array is empty.

Get the Updated Swift Array Count After Adding or Removing Elements

A Swift array is a variable-size collection. Its count changes as elements are appended, inserted, or removed.

</>
Copy
var scores = [10, 20, 30]

print(scores.count)

scores.append(40)
print(scores.count)

scores.remove(at: 1)
print(scores.count)

Output

3
4
3

Appending 40 increases the array size to four. Removing the element at index 1 reduces it to three.

Get Row and Element Counts in a Multidimensional Swift Array

A two-dimensional Swift array is an array whose elements are also arrays. The outer count gives the number of rows. Each inner array has its own count.

</>
Copy
let matrix = [
    [1, 2, 3],
    [4, 5, 6]
]

let rowCount = matrix.count
let firstRowCount = matrix[0].count
let totalElementCount = matrix.reduce(0) { total, row in
    total + row.count
}

print("Rows: \(rowCount)")
print("Elements in first row: \(firstRowCount)")
print("Total elements: \(totalElementCount)")

Output

Rows: 2
Elements in first row: 3
Total elements: 6

Do not assume that every row has the same number of elements. Swift also permits nested arrays with different row sizes, so calculate each row’s count when working with irregular data.

Create a Swift Array with a Required Initial Size

Swift arrays do not require a fixed length when declared. To create an array that initially contains a specific number of identical values, use Array(repeating:count:).

</>
Copy
var values = Array(repeating: 0, count: 5)

print(values)
print(values.count)

Output

[0, 0, 0, 0, 0]
5

The array starts with five elements, but it remains mutable and can grow or shrink later when declared with var.

Swift Array Count and Capacity Are Different

count is the number of elements currently stored in the array. Capacity is internal storage reserved for elements and is not the array’s logical size. Calling reserveCapacity(_:) can prepare storage for expected additions, but it does not add elements and does not change count.

</>
Copy
var numbers: [Int] = []

numbers.reserveCapacity(100)

print(numbers.count)

numbers.append(25)
print(numbers.count)

Output

0
1

For the property definition and related collection behavior, refer to Apple’s Array count documentation.

Common Swift Array Size Mistakes

  • Using length or size(): Swift arrays use the count property.
  • Calling count as a method: write numbers.count, not numbers.count().
  • Treating the count as the last valid index: indexes start at zero, so the last valid index of a nonempty array is count - 1.
  • Reading the first row of an empty nested array: check matrix.isEmpty before accessing matrix[0].
  • Confusing reserved capacity with element count: reserveCapacity(_:) does not populate the array.

Frequently Asked Questions about Swift Array Size

How do I get the size of an array in Swift?

Read the array’s count property. For example, numbers.count returns the number of elements currently stored in numbers.

Does a Swift array use size, length, or count?

A Swift array uses count. It does not provide JavaScript-style length or Java-style array length syntax.

How do I find the last valid index from a Swift array count?

For a nonempty array, the last valid index is array.count - 1. In ordinary code, array.indices.last or array.last can be safer because they account for an empty array.

How do I count all values in a two-dimensional Swift array?

Add the count of every inner array. One approach is matrix.reduce(0) { $0 + $1.count }, which also works when rows have different sizes.

Can I create a fixed-size array in Swift?

You can create an array with an initial number of elements using Array(repeating:count:). A standard Swift Array is still a variable-size collection, so code can add or remove elements when it is declared with var.

Editorial QA Checklist for Swift Array Count Examples

  • Verify that every example uses count as a property without parentheses.
  • Confirm that output values match the number of elements present after each append or removal.
  • Check that zero-based indexing is explained wherever count - 1 is used.
  • For multidimensional arrays, distinguish row count, per-row count, and total element count.
  • Confirm that examples do not describe reserveCapacity(_:) as adding elements or setting the logical array size.

Swift Array Size Summary

Use array.count to get the number of elements in a Swift array and array.isEmpty to test whether it contains no elements. For nested arrays, inspect the outer and inner counts separately, or add the row counts to calculate the total number of stored values. Continue with this Swift Tutorial for related array operations.