Cannot preview SwiftUI

I am running Catalina (10.15) with XCode11. After creating a new project and selecting the option to Use SwiftUI, the file opens, but i get an error message in the preview window;


'Cannot preview in this file -- active scheme does not build this file. '


the info button says 'Select a scheme that builds a target which contains the current file, or add this file to a target that is built by the current scheme.'


i've tried opening numerous new projects, with and without unit tests, but none of them allow it to be previewed. I did see on Twitter that I'm not alone in seeing this issue. Any ideas on steps to troubleshoot it?


Thanks all.

Post not yet marked as solved Up vote post of xianritchie Down vote post of xianritchie
13k views

Answers

I figured out the issue. As an update, i've tried a couple of suggestions from Twitter:


- unselecting the unit test options when creating the project.

- running

sudo xcodebuild -license

and going through the license agreement


Created a new project, and it wa still not allowing a preview. Then I realized that my Documents folder is actually being stored in iCloud Drive. I went back to my root user directory on my local HD and created a new folder in my user folder for the project, and after creating it, it worked without issue.

"Created a new project, and it wa still not allowing a preview. Then I realized that my Documents folder is actually being stored in iCloud Drive. I went back to my root user directory on my local HD and created a new folder in my user folder for the project, and after creating it, it worked without issue."


This solved the issue for me! I created a new project in a "Development" Folder in ~ and am off to the races! Thanks!

THank you!

Had the same problem and this fixed it in an instant

You saved my time. thx : )

What worked for me is I went to file inspector in the utilities area on the right hand side of the xcode editor. One of the files was there with a .swift extension. Type: Default - Swift Source. Changing the location is what worked for me. I changed it from "Relative to Group" to "Relative to Project" and the preview came up!

Potentially a slightly different problem but if you are using a Swift Package which has previews within it.


- The previews must be in the root folder of your sources, e.g. 'Sources/YourFramework/Previews.swift'.
- The active target must be an application target.


Then the previews magically work.

I use FireStore and what worked for me was to comment out...

FirebaseApp.configure()

in AppDelegate.swift.

Compile and magically works.
I don't know if this is that same problem, but I was trying to edit a file Foo.swift and the preview wouldn't work. It complained about another file BarTest.swift not being in any target. Now BarTest.swift was a test file in a package that was being included in my project. It didn't have anything to do with anything happening in Foo.swift, BUT I did have it up in a tab in my Xcode editor. When I closed that tab, the error went away and I was able to preview again. This was in Xcode 12.2.
  • I have a workspace containing both Swift Packages and a SwiftUI iOS client project, and while having the Swift package code open in a tab, I was unable to run previews for the client app. Closing the tab as you suggested fixed the issue for me. Thanks!

  • Some here on Xcode 12.5.

Add a Comment
Hello Developers,
here on my current configuration (Big Sur 11.1 (20C69), Xcode 12.3 (12C33)) I still have an problem
Code Block
withCouldNotFindInputNodeInBuiltGraphError<XcodeGraphSource, PreviewTranslationUnit>: WeeklyPlanViewModelTests.swift not found in any targets
WeeklyPlanViewModelTests.swift must belong to at least one target in the current scheme in order to use previews

and no of my changes bring me to get preview running again.

Clearing the project-build, deleting complete "DerivedData", also deactivating of this unittest-file and complete removing of unittest-target from the project: nothing helped to fix the preview-functionality.

What can I do in addition to get preview run again?
Interesting fact is, when creating an project new, copy all the files (without any tests) inside the project, and try out the preview, let it work some time. But I cannot develop without testing. After git-commit and inserting some testing files, it works one-two times, then it stops. And no git-reverting to the previous commit, brings preview again to life.

Kind regards,
Anton
@f_anton You can try to delete the WeeklyPlanViewModelTests.swift from your tests target by "Remove Reference", and add back again, It should work. At least in my case.