Index out of range 🍎 iOS

Swift: Fatal error: Index out of range

Code accessed an array index that doesn’t exist — empty arrays, off-by-one loops, or data changing while a table reloads.

Seen on: iOS

Meaning

Swift arrays trap on invalid indices. Table/collection data sources returning counts that don’t match the array, async updates during scrolling, and indices from another array are common.

Common causes

  • Empty array accessed with [0]
  • Off-by-one loop bounds
  • numberOfRows not matching the data array
  • Data mutated on another thread

⚡ Quick fix

  1. Use first/last or a safe subscript
  2. Keep data source counts in sync and update on the main thread
  3. Check bounds before access

Detailed fix by platform

Swift

  1. swift
    extension Collection { subscript(safe i: Index) -> Element? { indices.contains(i) ? self[i] : nil } }
    let item = items[safe: indexPath.row]

How to diagnose

  1. Index — Value vs count
  2. Source — Which array?
  3. Threading — Modified off the main thread?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.