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
- Use first/last or a safe subscript
- Keep data source counts in sync and update on the main thread
- Check bounds before access
Detailed fix by platform
Swift
- swift
extension Collection { subscript(safe i: Index) -> Element? { indices.contains(i) ? self[i] : nil } } let item = items[safe: indexPath.row]
How to diagnose
- Index — Value vs count
- Source — Which array?
- 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:
Similar errors
- attempted to access privacy-sensitive data without a usage description This app has crashed because it attempted to access privacy-sensitive data without a usage description
- Cannot convert value of type Swift: Cannot convert value of type 'X' to expected argument type 'Y'
- Could not cast value of type Swift: Could not cast value of type 'X' to 'Y'
Most viewed in iOS
Other ways to find it
🧠 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.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 7 Oct 2026