Chapter 39: Swift Arrays: Indices & Bounds

Array Indices & Bounds in Swift.

Imagine we’re sitting together with a playground open. I’m going to explain this topic slowly and thoroughly — because indices and bounds are one of the places where beginners crash most often, and understanding them properly will save you many headaches.

We will cover:

  • What indices really are
  • Why arrays are 0-based
  • How to safely access elements
  • All the safe ways to work with indices
  • Common crash patterns and how to avoid them
  • Real-world patterns you’ll actually use in apps

Let’s go step by step.

1. The Most Important Fact: Swift Arrays are Zero-Based

The first element is always at index 0.

Swift

So:

Swift

Trying to access anything outside this range crashes at runtime:

Swift

This is not optional — it’s a deliberate design choice for performance and clarity.

2. The Three Most Important Index-Related Properties

Every Array (and most collections) has these:

Swift

Key rule:

  • Valid indices are always from startIndex (inclusive) to endIndex (exclusive)
  • endIndex is never a valid index — it’s “one past the end”

3. Safe Ways to Access Elements (avoid crashes)

Way 1 – Use .first and .last (safest)

Swift

Way 2 – Check with indices.contains(_:)

Swift

Way 3 – Use optional subscript (very clean & modern)

Swift

Many real projects add this extension — it’s very convenient.

Way 4 – Use dropFirst, dropLast, prefix, suffix

These return slices and never crash:

Swift

4. Real-Life Examples – how slices & indices are used

Example 1 – Pagination / “show next page”

Swift

Example 2 – Recent items list (very common in UI)

Swift

Example 3 – Show first few items + “and more…”

Swift

Output (when > 3):

text

5. Very Common Beginner Mistakes & Correct Habits

Mistake Wrong / Dangerous code Correct / Safe habit Why?
Assuming slice starts at index 0 slice[0] after array[3…] slice.startIndex or slice.first Slice has its own indices
Force-unwrapping first / last array.first! array.first ?? default or if let Empty array → crash
Using negative indices array[-1] array.last or array[array.count-1] Negative indices crash
Keeping large slice when original is gone let slice = bigArray[0..<10]; bigArray = [] let copy = Array(slice) Slice keeps original alive in memory
Modifying original while holding slice let slice = arr[0..<5]; arr.append(99) Be aware — slice sees the change Shared storage (can be desired or surprising)

6. Quick Reference – Most Used Safe Index & Slice Patterns

Goal Recommended code Returns type Safe?
First item safely array.first ?? default Element? Yes
Last item safely array.last ?? default Element? Yes
First N items array.prefix(N) or array[..<N] ArraySlice Yes
Last N items array.suffix(N) ArraySlice Yes
Skip first N array.dropFirst(N) ArraySlice Yes
Skip last N array.dropLast(N) ArraySlice Yes
Check if index is valid array.indices.contains(index) Bool Yes
Safe subscript array[safe: index] (custom extension) Element? Yes
Convert slice to independent array Array(slice) [Element] Yes

7. Small Practice – Try these

  1. Create array of 10 numbers (1…10) → print items 3 to 7 (inclusive) using range → print them using enumerated() inside the slice
  2. Create array of 6 names → show only the last 3 names using suffix(3) → number them as “Recent 1: …”, “Recent 2: …”, etc.
  3. Create array of 8 tasks → show first 4 using prefix(4) → show “and (remaining) more…” if there are more

Paste your attempts if you want feedback or better ways!

What would you like to explore next about arrays?

  • Sorting arrays (simple & custom comparators)
  • Filtering, mapping, reducing in depth
  • Multidimensional arrays (array of arrays)
  • Array vs ArraySlice lifetime & memory
  • Arrays in SwiftUI (List, ForEach, diffable data sources…)
  • Or move to another topic (dictionaries, sets, strings…)

Just tell me — we’ll keep going in the same detailed, patient, teacher-like style 😊

You may also like...

Leave a Reply

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