Fatal error: Unexpectedly found nil while unwrapping an Optional value
Swift force-unwrapped (!) an optional that was nil, crashing the app.
Seen on:
iOS
Meaning
Force unwraps and implicitly unwrapped optionals (IBOutlets) crash when the value is missing. Xcode stops on the exact line; common sources are disconnected outlets, failed casts, missing resources and decoding failures.
Common causes
- IBOutlet not connected or accessed before viewDidLoad
- Force-unwrapping
URL(string:),UIImage(named:)or a dictionary lookup - Forced cast
as!on unexpected type - Storyboard identifier mismatch when instantiating a view controller
⚡ Quick fix
- Replace
!withguard let/if letand handle nil - Reconnect the outlet in Interface Builder
- Check resource names, storyboard IDs and URL strings
Detailed fix by platform
iOS
- Safe unwrapping:swift
guard let url = URL(string: endpoint) else { assertionFailure("Bad endpoint: \(endpoint)") return } if let image = UIImage(named: "avatar") { avatarView.image = image }
How to diagnose
- Line — Which expression is force-unwrapped?
- Source — Outlet, resource, cast or data?
- Timing — Accessed before it was set?
🧠 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 2 Oct 2026