Xcode: Cannot preview in this file — Update failed

Xcode: Cannot preview in this file — Update failed

Issue

Playing with SwiftUI, I have faced this error few times and for the first time it took me some time to resolve it. So here is what I found as a solution to resolve this issue.

PotentialCrashError: Update failed

your-project.app may have crashed. Check ~/Library/Logs/DiagnosticReports for any crash logs from your application.

==================================

|  Error Domain=com.apple.dt.UITestingAgent Code=-1 "failed to load library at path "/Users/**********/Documents/your-project/Build/Intermediates.noindex/Previews/your-project/Intermediates.noindex/your-project.build/Debug-iphonesimulator/your-project.build/Objects-normal/x86_64/DetailView.2.preview-thunk.dylib": Optional(dlopen(/Users/**********/Documents/your-project/Build/Intermediates.noindex/Previews/your-project/Intermediates.noindex/your-project.build/Debug-iphonesimulator/your-project.build/Objects-normal/x86_64/DetailView.2.preview-thunk.dylib, 2): no suitable image found.  Did find:
|  	/Users/**********/Documents/your-project/Build/Intermediates.noindex/Previews/your-project/Intermediates.noindex/your-project.build/Debug-iphonesimulator/your-project.build/Objects-normal/x86_64/DetailView.2.preview-thunk.dylib: open() failed with errno=1)" UserInfo={NSLocalizedDescription=failed to load library at path "/Users/**********/Documents/your-project/Build/Intermediates.noindex/Previews/your-project/Intermediates.noindex/your-project.build/Debug-iphonesimulator/your-project.build/Objects-normal/x86_64/DetailView.2.preview-thunk.dylib": Optional(dlopen(/Users/**********/Documents/your-project/Build/Intermediates.noindex/Previews/your-project/Intermediates.noindex/your-project.build/Debug-iphonesimulator/sketch-elements.build/Objects-normal/x86_64/DetailView.2.preview-thunk.dylib, 2): no suitable image found.  Did find:
|  	/Users/**********/Documents/your-project/Build/Intermediates.noindex/Previews/your-project/Intermediates.noindex/your-project.build/Debug-iphonesimulator/your-project.build/Objects-normal/x86_64/DetailView.2.preview-thunk.dylib: open() failed with errno=1)}

Solution

  1. Close Xcode
  2. Delete Build and DerivedData folders in your project path
  3. Restart your mac
  4. Build your project with the ▶ button

In case you don’t see the preview click Editor -> Canvas.

References

Buy me a coffeeOut of coffee 😱, please help!