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.

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.
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.

What fixed it for me was copying the Xcode project file and the folder of code into a separate housing folder, and opening that as a separate project - the process in the original post was unnecessary.

In Xcode "Product -> Clean Build Folder" worked for me.

Restart your mac worked for me

You have to store the Project Folder on local disk and if you store in a server or Apple iCloud it's not gonna work

I had this problem when I added a folder and was using the cloud storage. I simply removed the folders added and added them again. Make sure to select both "Create Groups" and "Create Items ..". Usually it is "Create a folder reference" by default. I hope this information can help someone looking for a quick solution.

Close all opened projects and open only one, cause it seems that "At most one Xcode project can be open that contains your shared SwiftUI views". https://developer.apple.com/forums/thread/673932

Using Xcode version 13.4.1 and faced the same problem when created new SwiftUI file. Xcode for some reading automatically turns off this toggle switch. Go to yourProject.xcodeproj -> Build Settings -> Build Options -> Enable Previews and toggle it back on to “Yes”.

PreviewProvider protocol is available from iOS 13 only. If you select the running target of your project to any simulator which is below iOS 13 the previews won't work. Just select anything iOS13 or above, pause and resume preview and it will work.

For me this was fixed by:

  1. Changing the build scheme from release to debug
  2. Clean build folder
  3. Build and launch on iPhone simulator.
  4. Stop running and close Xcode
  5. Reopen Xcode and click refresh on preview

This fixed it for me:

  • Quitting xCode
  • Starting xCode

When you start xCode/open a project, it takes a while in the upper toolbar to build the project, also the previews. As I let that run to the end (takes couple of seconds on Macbook Air M1), everything was fine. No changes needed.

Changing preview mode from "My mac" to iPhone Dynamic Island worked for me

Cannot preview SwiftUI
 
 
Q