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
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 arrayExplanation
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
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' arrayOutput
2
false
["banana", "cherry"]Key Takeaways
- Arrays are ordered, zero-indexed collections that allow duplicate values.
- Use
varfor mutable arrays andletfor arrays that must never change. letarrays are fully immutable — no appending, removing, or mutating elements..append(),.remove(at:),.count, and.isEmptyare the core array operations.- Arrays are value types with copy-on-write semantics, so copies are cheap until modified.
Practice what you learned
1. What happens if you try to call `.append()` on an array declared with `let`?
2. What index does the first element of a Swift array have?
3. Which method removes the element at a specific index from a mutable array?
4. Do Swift arrays allow duplicate values?
5. What Swift optimization allows array copies to be cheap until modification?
Was this page helpful?
You May Also Like
Dictionaries and Sets in Swift
Understand Swift's key-value dictionaries and unique-element sets, including how Hashable powers both.
Collection Operations in Swift (map, filter, reduce)
Master Swift's functional-style collection operations — map, filter, reduce, compactMap, and sorted.
Loops in Swift (for-in, while, repeat-while)
Understand Swift's three loop types — for-in, while, and repeat-while — since the C-style for loop was removed.
Generics in Swift
Understand how Swift generics let you write flexible, reusable, type-safe code that works with any type.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics