Fatal error: Duplicate values for key: 'id'
Creating a dictionary with duplicate keys traps.
Exact message you may see
Fatal error: Duplicate values for keyDictionary uniqueKeysWithValues duplicate key crashFatal error: Duplicate values for key: 'id' Meaning
Dictionary(uniqueKeysWithValues:) crashes on duplicates. Use Dictionary(_:uniquingKeysWith:) or grouping:.
Common causes
- Duplicate ids in input
- uniqueKeysWithValues used
- Data not cleaned
⚡ Quick fix
- Use
Dictionary(items.map{($0.id,$0)}, uniquingKeysWith: { a, _ in a }) - Deduplicate first
- Validate server data
Detailed fix by platform
Swift
let map = Dictionary(items.map { ($0.id, $0) }, uniquingKeysWith: { first, _ in first })
How to diagnose
- Duplicates — in data?
- Initializer — unique?
- Merge — rule?
🔧 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
- AVAudioEngine: required condition is false: format.sampleRate == hwFormat.sampleRate Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: format.sampleRate == hwFormat.sampleRate'
- Cannot convert value of type Swift: Cannot convert value of type 'X' to expected argument type 'Y'
- Core Data: NSManagedObjectContext was accessed from the wrong queue CoreData: error: Serious application error. Exception was caught during Core Data change processing
Most viewed in iOS
- SIGABRT Thread 1: signal SIGABRT (app crashes with "libc++abi: terminating")
- ITMS-91053 ITMS-91053: Missing API declaration — Your app's code references required reason APIs (privacy manifest)
- integrity could not be verified Unable to Install "App": This app cannot be installed because its integrity could not be verified
Other ways to find it
Where this error sits
Origin: On your device. Errors that are often confused with or caused by this one (click to open):
🧠 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.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Did this fix work for you?
Report a correction or suggest an improvement
Last updated 25 Nov 2026