'SwiftUI cannot preview this file - unexpected error occurred
A UIKit Xcode project targeted for only iOS, it has 3 targets for different build configurations.
When I add a swiftui file it builds and runs fine, but I can't see the simplest previews I get Cannot preview this file - un expected error occurred
Exact diagnostics is
HumanReadableSwiftError
ProviderError: ProviderError: noPreviewInfos(runDestination: Simulator - iOS 15.2 | iphonesimulator | x86_64 | iPhone 13 Pro Max | Apple Watch Series 6 - 44mm, thunkConfiguration: <IDEBuildOperationConfiguration buildCommand: 8, buildParameters: <IDEBuildParameters:0x7fe4bffe2f40:action=build:command=<IDEConcretePrimitiveSchemeCommand:0x7fe4bb382500:Run>:config=Release:dest=iPhone 13 Pro Max:arch=x86_64:workspaceArena=<IDEWorkspaceArenaSnapshot: 0x7fe4bbbbf2b0>:
overridingProperties=(
{ // Preview overrides (immutable)
DSTROOT = 【𝗟𝗶𝘁𝗲𝗿𝗮𝗹𝗦𝘁𝗿𝗶𝗻𝗴:/Users/x/Library/Developer/Xcode/DerivedData/MayApp-ezbgeibumcswldddlegmcbpnukij/Build/Intermediates.noindex/Previews/MayApp/ReleaseProducts】
ENABLE_PREVIEWS = 【YES】
OBJROOT = 【𝗟𝗶𝘁𝗲𝗿𝗮𝗹𝗦𝘁𝗿𝗶𝗻𝗴:/Users/x/Library/Developer/Xcode/DerivedData/MayApp-ezbgeibumcswldddlegmcbpnukij/Build/Intermediates.noindex/Previews/MayApp/Intermediates.noindex】
SYMROOT = 【𝗟𝗶𝘁𝗲𝗿𝗮𝗹𝗦𝘁𝗿𝗶𝗻𝗴:/Users/x/Library/Developer/Xcode/DerivedData/MayApp-ezbgeibumcswldddlegmcbpnukij/Build/Intermediates.noindex/Previews/MayApp/Products】
},
):
collectBuildTimeStatistics=NO
collectTimelineMetrics=NO>, executionEnvironment: Optional(<IDEExecutionEnvironment: 0x7fe52c865050>), singleFileToBuild: nil, buildLog: nil, parallelizeBuildables: true, dontActuallyRunCommands: false, buildImplicitDependencies: true, skipDependencies: false, buildables: Optional([<Xcode3TargetProduct:0x7fe549f34fa0:MayApp.app blueprint:<Xcode3Target:0x7fe4bb385f20:MayApp>>]), subsetOfBuildables: nil, buildDescriptionID: nil, scheme: Optional(<IDEScheme:0x7fe4baea1490:'MayApp'>), schemeTask: 0, clientInfo: nil>, blueprintName: MayApp Staging, sourceFilePath: /Users/x/Git/MayApp/options/MayApp_ios/MayApp/Application/Exploration/ProfileSettings/ProfileSettingsView.swift, thunkVariantSuffix: __XCPREVIEW_THUNKSUFFIX__)
Things I have tried: I updated macos and xcode to latest version (Xcode Version 13.2.1)
- SwiftUI file is added to all 3 targets
- Erased the simulators
- Deleted derived data
- Restarted the mac and xcode multiple times
- Tried setting the Build Active Architectures to both Yes and No
- Excluded architecture arm64
sudo xcodebuild -license accept- Tried setting
ENABLE_PREVIEWS = YESfor all targets as well
Nothing works
Weirdly it also complained about the UI tests when I added SwiftUI file without running the test, but I commented out those files now.
Solution 1:[1]
What libraries/packages are you using? I encountered a similar error for a long period of time. I was using the Package Manager to implement Firebase functionality, and was able to compile/run normally but not for previews or simulators specifically. None of the excluded architecture stuff worked for me either. Instead, I had to switch back to using Cocoapods rather than the Package Manager, at least for Firebase specifically. After some fiddling, I've never had an issue since and run previews all the time. Apologies if this is not useful but you didn't provide much context.
Solution 2:[2]
Usually, this kind of issue gets resolved by clearing up the derived data folder. Below are the steps I usually follow:
- Deep clean the project by using Cmd+Alt+Shft+K
- Quit Xcode
- Clear derived data folder
- Restart Mac(Not required always)
- Restart Xcode
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vera Gonzalez |
| Solution 2 | shim |
