100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Swift

Arrays in Swift

Learn how Swift arrays store ordered, mutable collections of values and how to manipulate them safely.

CollectionsBeginner8 min readJul 8, 2026
Analogies

Introduction

An array is an ordered collection that can store multiple values of the same type. Arrays in Swift are zero-indexed, meaning the first element sits at index 0, and they allow duplicate values. Because Swift arrays are value types, assigning an array to a new variable or passing it to a function creates a logically independent copy, though Swift uses copy-on-write under the hood so the actual copy only happens when the data is modified.

🏏

Cricket analogy: A T20 batting order is an array indexed from 0, so the opener sitting at position 0 is Rohit Sharma; if you hand the lineup card to a co-selector, they get their own copy, but Swift only actually duplicates the paper once someone actually edits it.

Syntax

swift
var numbers: [Int] = [1, 2, 3]
var names = ["Alice", "Bob"]        // type inferred as [String]
var empty: [Double] = []             // empty array literal
let fixed: [Int] = [10, 20, 30]      // immutable array

Explanation

Declaring an array with var allows it to grow, shrink, or have its elements changed after creation. Common operations include .append(_:) to add an element at the end, .remove(at:) to delete an element at a given index, .count to check the number of elements, and .isEmpty to test whether the array has any elements at all. A crucial Swift-specific detail is that arrays declared with let are fully immutable: you cannot append, remove, or mutate their elements, and attempting to do so is a compile-time error. This differs from some other languages where only the reference is fixed but the contents can still change.

🏏

Cricket analogy: A var squad list lets the selectors .append(_:) a new fast bowler or .remove(at:) an injured player before the match, but if the final XI is declared with let, trying to swap a player after the toss is a compile-time error, not just a rule violation.

Example

swift
var fruits = ["apple", "banana"]
fruits.append("cherry")
fruits.remove(at: 0)
print(fruits.count)
print(fruits.isEmpty)
print(fruits)

let lockedFruits = ["mango", "kiwi"]
// lockedFruits.append("pear") // Compile error: cannot mutate a 'let' array

Output

swift
2
false
["banana", "cherry"]

Key Takeaways

  • Arrays are ordered, zero-indexed collections that allow duplicate values.
  • Use var for mutable arrays and let for arrays that must never change.
  • let arrays are fully immutable — no appending, removing, or mutating elements.
  • .append(), .remove(at:), .count, and .isEmpty are the core array operations.
  • Arrays are value types with copy-on-write semantics, so copies are cheap until modified.

Practice what you learned

Was this page helpful?

Topics covered

#Swift#SwiftProgrammingStudyNotes#Programming#ArraysInSwift#Arrays#Syntax#Explanation#Example#DataStructures#StudyNotes#SkillVeris