Swift
array
programming
add element
tutorial

Add an element to an array in Swift

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Swift arrays are ordered, type-safe collections, and adding elements is one of the most common array operations. The method you choose depends on whether you are appending one value, inserting at a specific index, or merging multiple values into the array.

Use append For One Element At The End

The most direct way to add one element is append:

swift
1var fruits = ["Apple", "Banana"]
2fruits.append("Orange")
3
4print(fruits)

This is the idiomatic answer when you want to grow the array at the end.

append is efficient for normal array-building code because Swift arrays are optimized for amortized growth at the tail.

Use append(contentsOf:) For Multiple Elements

If you want to add another collection of elements, use append(contentsOf:):

swift
1var primes = [2, 3, 5]
2primes.append(contentsOf: [7, 11, 13])
3
4print(primes)

You can also use +=:

swift
1var animals = ["Cat", "Dog"]
2animals += ["Bird"]
3
4print(animals)

Both are valid, but append(contentsOf:) is often clearer when the intent is "append this collection."

Use insert(_:at:) For A Specific Position

When the new element belongs somewhere other than the end, use insert(_:at:):

swift
1var letters = ["A", "C", "D"]
2letters.insert("B", at: 1)
3
4print(letters)

This shifts later elements to the right. Because of that shift, insertion in the middle is generally more expensive than appending at the end.

That cost matters in tight loops or large arrays, but not in ordinary application code.

Arrays Must Be Mutable

You can only add elements to an array declared with var:

swift
let fixed = [1, 2, 3]
// fixed.append(4)  // Compile-time error

To modify it:

swift
var numbers = [1, 2, 3]
numbers.append(4)

This is a basic but important Swift rule: let creates an immutable value.

Type Safety Still Applies

Swift arrays hold values of one element type. If the array is [Int], you cannot append a String:

swift
var scores: [Int] = [10, 20, 30]
scores.append(40)

This type safety is helpful because many array mistakes are caught at compile time instead of surfacing later at runtime.

If you need mixed values, Swift expects you to model that more explicitly instead of quietly mixing unrelated types.

Performance And Capacity

Appending is usually fast, but repeated growth may cause reallocation when the array runs out of reserved storage. If you already know roughly how many elements you will add, reserve capacity up front:

swift
1var values: [Int] = []
2values.reserveCapacity(1000)
3
4for i in 0..<1000 {
5    values.append(i)
6}

This is not necessary for everyday code, but it can help in performance-sensitive loops.

Another practical pattern is appending conditionally:

swift
1var messages: [String] = []
2let errorText: String? = "Network timeout"
3
4if let errorText {
5    messages.append(errorText)
6}

That keeps array-building code clean when values may or may not exist at runtime.

Common Pitfalls

One common mistake is trying to append to an array declared with let.

Another issue is using insert(_:at:) with an invalid index, which causes a runtime trap.

A third problem is assuming += and append are interchangeable in every case. append adds one element; append(contentsOf:) and += add multiple elements from another collection.

Finally, developers sometimes overthink array growth when a simple append would be perfectly fine. Most application code should start with the straightforward API and optimize only if profiling says it matters.

Summary

  • Use append to add one element to the end of a Swift array.
  • Use append(contentsOf:) or += to add multiple elements.
  • Use insert(_:at:) when the new element belongs at a specific position.
  • Arrays must be declared with var if you want to modify them.
  • Reserve capacity only when you have a real performance reason to do so.

Course illustration
Course illustration

All Rights Reserved.