If Xcode shows “Preview crashed” for a SwiftUI view, the problem may come from a runtime crash in the view itself, missing preview dependencies, sample data, Swift Packages, the Simulator runtime, Derived Data, or Xcode Previews. This guide walks through practical checks and fixes in a useful troubleshooting order.
- Common causes of “Preview crashed”
- Typical situation
- What to check first
- Fix 1: Reduce the preview to a minimal case
- Fix 2: Provide required environment dependencies
- Fix 3: Look for runtime crashes in preview data
- Fix 4: Restart the canvas
- Fix 5: Clear Derived Data
- Fix 6: Check Simulator state
- Fix 7: Check Swift Packages and frameworks
- Fix 8: Avoid external I/O during preview initialization
- If the preview still crashes
- Summary
Common causes of “Preview crashed”
- A runtime error such as a force unwrap or out-of-range array access occurs while the preview is running.
- A required
@Environment,@EnvironmentObject, SwiftData, or Core Data dependency is missing. - Preview sample data throws an error or calls
fatalError. - A Swift Package or framework fails to link at preview runtime.
- The Simulator runtime or preview simulator is in a bad state.
- Derived Data or preview build artifacts are stale or inconsistent.
- Xcode Previews itself is temporarily failing.
Typical situation
A preview can crash even when the app builds and runs normally. For example:
#Preview {
ContentView()
.environmentObject(AppModel())
}
If ContentView also expects another environment value or a persistence container that the preview does not provide, the canvas can terminate during launch.
What to check first
- Run the same view in the Simulator and see whether the app itself crashes.
- Open the detailed error shown in the preview canvas.
- Use Editor > Canvas > Diagnostics to inspect preview diagnostics.
- Reduce the preview to the smallest possible configuration, then add dependencies back one at a time.
- Check the available Simulator devices and runtimes.
Fix 1: Reduce the preview to a minimal case
Temporarily remove mock data and environment configuration to see whether the view can render by itself.
#Preview {
ContentView()
}
If this works, one of the dependencies previously added to the preview is the likely cause.
Fix 2: Provide required environment dependencies
If the view uses @EnvironmentObject, inject the required object in the preview too.
#Preview {
ContentView()
.environmentObject(AppModel())
}
For SwiftData, an in-memory model container is useful for previews because it avoids depending on production data.
#Preview {
let config = ModelConfiguration(isStoredInMemoryOnly: true)
let container = try! ModelContainer(for: Item.self, configurations: config)
return ContentView().modelContainer(container)
}
Fix 3: Look for runtime crashes in preview data
Previews execute your code, so unsafe assumptions can crash the canvas.
let value = optionalValue!
let first = items[0]
Check whether preview data can be nil or empty and prefer safe access.
if let value = optionalValue { Text(value) }
if let first = items.first { Text(first.title) }
Fix 4: Restart the canvas
Sometimes the preview process becomes unstable even when the source code is valid. Use the Restart control in the canvas, or hide and show the canvas again.
Apple’s current Xcode documentation also describes restarting the canvas when a preview is paused or needs to run again.
Fix 5: Clear Derived Data
Stale build products can prevent previews from launching correctly. Remove Derived Data and rebuild the project.
rm -rf ~/Library/Developer/Xcode/DerivedData/*
Then restart Xcode, build the project, and open the preview again.
Fix 6: Check Simulator state
SwiftUI previews depend on Simulator runtimes, so a broken runtime or stale device can also cause crashes.
xcrun simctl list devices
xcrun simctl list runtimes
You can remove devices that are no longer usable with:
xcrun simctl delete unavailable
Fix 7: Check Swift Packages and frameworks
If preview diagnostics show messages such as “Runtime linking failure” or “Symbols not found,” the problem may be a dependency rather than the SwiftUI view. Resolve package dependencies again and confirm that the package supports the preview target’s platform and architecture.
xcodebuild -resolvePackageDependencies
Fix 8: Avoid external I/O during preview initialization
Network requests, Keychain access, file access, and third-party service initialization can behave differently in the preview environment. Prefer deterministic mock data for previews and avoid starting external I/O directly from view initialization.
If the preview still crashes
If even a minimal view crashes, restart Xcode and make sure you are using a current stable Xcode release and compatible Simulator runtime. If the issue remains reproducible, generate a diagnostics report from Editor > Canvas > Diagnostics and attach it when filing Feedback with Apple.
A successful app build does not guarantee that the preview runtime will succeed. If the app runs in Simulator but the preview alone crashes, prioritize preview-only dependencies, Simulator state, runtime linking, and Xcode Preview diagnostics.
Summary
“Preview crashed” is commonly caused by runtime errors, missing environment dependencies, preview data, package linking, Simulator problems, or stale build artifacts. Start with a minimal preview, inspect Diagnostics, and then restore dependencies one by one. That process usually reveals whether the failure comes from your view code or from the preview environment itself.
