JBrown
JBrown

Reputation: 939

SwiftUI Preview error - "Cannot preview in this file - Update failed"

I created a new SwiftUI project but the default code will not load in the live preview window. I get the following error every time: "Cannot preview in this file - Update failed". I am running Xcode 11.4 on Catalina 10.15.4

Image of the error in Xcode Image of the error in Xcode

Image of the diagnostics report Image of the diagnostics report

Here is a list of the things that I have tried so far:

1) Restarted Xcode and my computer

2) Cleaned the build folder of the project

3) Deleted the build folder and reran the project

4) Changed the simulator device (I've tried Generic iOS device, iPhone 11, 11 pro max, etc.)

5) Changed the project location to desktop

Any help would be much appreciated!

Upvotes: 9

Views: 5901

Answers (5)

Njuacha Hubert
Njuacha Hubert

Reputation: 566

For me, I tried all the above and the solution was when I updated xcode to the latest version

Upvotes: 0

Umut ADALI
Umut ADALI

Reputation: 1196

It should be a lot of different problem. One of them if the "Build configuration" is Release then there should go -Onone error. You should change Release to Debug in the scheme. Edit scheme dialog

Upvotes: 0

Haseeb Iqbal
Haseeb Iqbal

Reputation: 1725

Below steps worked for me.

  1. In Xcode, clean build folder
  2. Quit Xcode
  3. Go to Derived-Data directory
  4. Delete Everything in SymbolCache.noindex directory under Derived Data
  5. Delete Everything in ModuleCache.noindex directory under Derived Data
  6. Find and delete project directory under Derived Data
  7. Go to /Users/$(whoami)/Library/Developer/Xcode/UserData/Previews/ and delete everything
  8. Empty your Bin
  9. Re-Open Xcode and start the preview

Upvotes: 0

brainray
brainray

Reputation: 12884

Do 2 steps:

  • Delete Previews inside (but not the Preview folder itself)

/Users/me.myself/Library/Developer/Xcode/UserData/Previews

run

killall -9 com.apple.CoreSimulator.CoreSimulatorService

Restart Xcode

Upvotes: 4

yrk
yrk

Reputation: 410

I had a similar issue, but in my case diagnostics showed: RemoteHumanReadableError

My fix was to quit Xcode (close window and quit from menu or use CMD+Q).

Delete contents of:

/Users/$(whoami)/Library/Developer/Xcode/DerivedData/

With something like this:

rm -rf /Users/$(whoami)/Library/Developer/Xcode/DerivedData/*

NOTE: System may ask you for confirmation, so you have to answer y and tap enter (always use rm -rf with caution!).

Then start Xcode again and open project - it took little longer to load but worked fine without issue.

I just hope this helps solving OP issue as well as other simillar issues with SwiftUI preview stupid errors not related to code.

Upvotes: 0

Related Questions