Chapter 36: Arrays

1. What is an Array in Swift? (the big picture)

An array is an ordered collection of values of the same type.

Think of it as:

  • a numbered list
  • a shopping list written in order
  • seats in a movie theater row (each seat has a number 0, 1, 2, …)

Important facts about Swift arrays:

  • Ordered — items keep the order you put them in
  • Indexed — first item is at index 0, second at 1, etc.
  • Type-safe — all elements must be the same type
  • Dynamic — can grow or shrink at any time (when using var)
  • Value type — when you assign or pass an array, a copy is made
  • Immutable when let — you can read items, but cannot add/remove/change them
  • Mutable when var — you can add, remove, change items
Swift

2. Creating Arrays — all the common ways

Swift

3. Reading from Arrays — the most frequent operations

Swift

Important safety note: Accessing invalid index crashes at runtime:

Swift

→ Always check bounds before using index:

Swift

4. Modifying Arrays — when using var

Swift

5. Looping over Arrays — the three most common styles

Style 1 — simplest (most used)

Swift

Style 2 — with index (very frequent)

Swift

Style 3 — when you only need the index

Swift

6. Very Common Real-Life Examples

Example 1 – Shopping cart / order list

Swift

Example 2 – To-do list with completion status

Swift

Example 3 – Scoreboard / leaderboard

Swift

7. Very Common Beginner Mistakes & Correct Habits

Mistake Wrong / Dangerous code Correct / Safe habit Why?
Force-unwrapping first/last fruits.first! fruits.first ?? “None” or if let Array can be empty → crash
Modifying let array let arr = [1]; arr.append(2) Use var if you need to change let arrays are immutable
Accessing index without bounds check arr[10] if arr.indices.contains(10) { … } Out-of-bounds → fatal crash
Mutating array while iterating for item in arr { arr.append(item) } Build new array or use indices Runtime error: collection mutated
Forgetting arrays are value types var a = b; a.append(1) → b changes Normal — copy is made Safety feature

8. Quick Reference – Most Used Array Operations

Goal Best modern code Returns / modifies original?
Add one item arr.append(item) modifies original
Add multiple arr.append(contentsOf: […]) modifies original
Insert at position arr.insert(item, at: index) modifies original
Remove at index arr.remove(at: index) returns removed item
Remove last arr.removeLast() returns removed item
Clear all arr.removeAll() modifies original
Check contains arr.contains(value) Bool
First / last (safe) arr.first / arr.last Optional
Loop with index for (i, item) in arr.enumerated() { … }
Filter arr.filter { $0 > 10 } new array
Transform arr.map { $0 * 2 } new array
Join to string arr.joined(separator: “, “) new string

9. Small Practice – Try these now

  1. Create an array of 5 favorite movies → print them numbered → add one more at the beginning → remove the last one → check if “Inception” is in the list
  2. Create array of numbers 1…10 → filter only even numbers → multiply each by 3 → join them with ” → ” separator

Paste your code here if you want feedback or corrections!

What would you like to explore next about arrays?

  • Array slicing (subarrays, prefix, dropFirst…)
  • Sorting arrays & custom sorting
  • Dictionaries (key-value collections)
  • Sets (unique values, no duplicates)
  • Advanced operations (reduce, compactMap, flatMap…)
  • Arrays in SwiftUI (List, ForEach, @State)

Just tell me — we’ll continue in the same clear, patient, detailed style 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *