Swift: Could not cast value of type 'X' to 'Y'
A force cast (as!) failed because the object isn’t the expected type.
Seen on:
iOS
Meaning
Often a storyboard view controller/cell whose Custom Class isn’t set, JSON decoded as a different type (NSNumber vs String), or a dequeued cell of another class.
Common causes
- as! with the wrong type
- Storyboard scene/cell Custom Class not set
- JSON value of unexpected type
- Wrong cell identifier → different cell class
⚡ Quick fix
- Use as? with a guard and log the actual type
- Set the Custom Class (and Module) in IB
- Use Codable with correct types
Detailed fix by platform
Swift
- swift
guard let vc = storyboard?.instantiateViewController(withIdentifier: "Detail") as? DetailViewController else { assertionFailure("Detail scene has wrong class"); return }
How to diagnose
- Types — Actual vs expected in the message
- IB — Custom Class set?
🔧 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
- 0xC0000142 The application was unable to start correctly (0xc0000142)
- 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'
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