Swift Arrays
An array is an ordered list of values of the same type. Think of it like a numbered shelf in a warehouse — each slot holds one item, and each slot has a position number called an index. Arrays start counting from index 0.
Creating an Array
Array Literal
let fruits = ["Apple", "Banana", "Cherry"]
let scores = [95, 88, 72, 60]
let flags = [true, false, true]Empty Array
var names: [String] = []
var counts = [Int]()Array With a Default Value
var zeros = Array(repeating: 0, count: 5)
print(zeros) // Output: [0, 0, 0, 0, 0]Diagram: Array Index Layout
Array: ["Apple", "Banana", "Cherry", "Date"] Index: 0 1 2 3 fruits[0] → "Apple" fruits[1] → "Banana" fruits[3] → "Date"
Accessing Elements
let cities = ["Tokyo", "London", "Paris"]
print(cities[0]) // Output: Tokyo
print(cities[2]) // Output: Paris
print(cities.first ?? "None") // Output: Tokyo
print(cities.last ?? "None") // Output: ParisAlways use first and last properties (which return optionals) to avoid out-of-range crashes.
Array Properties
var items = ["Pen", "Book", "Bag"]
print(items.count) // Output: 3
print(items.isEmpty) // Output: falseModifying an Array
Append — Add to the End
var colors = ["Red", "Green"]
colors.append("Blue")
print(colors) // Output: ["Red", "Green", "Blue"]Insert — Add at a Position
colors.insert("Yellow", at: 1)
print(colors) // Output: ["Red", "Yellow", "Green", "Blue"]Remove — Delete an Element
colors.remove(at: 0)
print(colors) // Output: ["Yellow", "Green", "Blue"]
colors.removeLast()
print(colors) // Output: ["Yellow", "Green"]Update — Change an Element
var animals = ["Cat", "Dog", "Bird"]
animals[1] = "Fish"
print(animals) // Output: ["Cat", "Fish", "Bird"]Iterating Over an Array
for-in Loop
let planets = ["Mercury", "Venus", "Earth", "Mars"]
for planet in planets {
print(planet)
}
// Output: Mercury Venus Earth MarsLoop With Index Using enumerated()
for (index, planet) in planets.enumerated() {
print("\(index + 1). \(planet)")
}
// Output:
// 1. Mercury
// 2. Venus
// 3. Earth
// 4. MarsUseful Array Methods
contains
let numbers = [10, 20, 30, 40]
print(numbers.contains(20)) // Output: true
print(numbers.contains(99)) // Output: falsesorted
let unsorted = [5, 1, 4, 2, 3]
let sorted = unsorted.sorted()
print(sorted) // Output: [1, 2, 3, 4, 5]
let descending = unsorted.sorted(by: >)
print(descending) // Output: [5, 4, 3, 2, 1]filter
let ages = [12, 19, 25, 14, 30]
let adults = ages.filter { $0 >= 18 }
print(adults) // Output: [19, 25, 30]map
let prices = [10.0, 20.0, 30.0]
let discounted = prices.map { $0 * 0.9 }
print(discounted) // Output: [9.0, 18.0, 27.0]reduce
let totals = [5, 10, 15]
let sum = totals.reduce(0) { $0 + $1 }
print(sum) // Output: 30Combining Arrays
let a = [1, 2, 3]
let b = [4, 5, 6]
let combined = a + b
print(combined) // Output: [1, 2, 3, 4, 5, 6]2D Arrays
An array of arrays creates a grid structure — useful for game boards, tables, and matrices.
var grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(grid[0][0]) // Output: 1 (row 0, column 0)
print(grid[1][2]) // Output: 6 (row 1, column 2)
print(grid[2][1]) // Output: 8 (row 2, column 1)Diagram: 2D Array as a Grid
Col 0 Col 1 Col 2
Row 0 [ 1 , 2 , 3 ]
Row 1 [ 4 , 5 , 6 ]
Row 2 [ 7 , 8 , 9 ]
grid[1][2] → Row 1, Col 2 → 6
Summary
Arrays store ordered lists of same-type values accessed by zero-based index. Use append, insert, remove, and direct index assignment to modify them. Power methods like filter, map, and reduce transform arrays in one line. Arrays are the most common collection type in Swift.
