Cannot preview in this file - active scheme does not build this file : SwiftUI on Xcode 11 in CatalinaOS

Viewed 14841

I opened Landmark App using SwiftUI on Xcode 11 in macOS Catalina(10.15) and while opening the Canvas Editor for .swift files containing SwiftUI is showing

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

Try Again, Diagonistics option or restarting Xcode not solving the problem.

12 Answers

If this is a new project coming from a copied folder and inside an iCloud folder, just close Xcode and relaunch it. The sync was not yet done.

Select the Scheme that has the current file to Preview
enter image description here

I experienced the same issue. All I did was to copy the "StartingPoint" folder out of the downloaded folder and relaunched the project. It worked!!!

You should go through Xcode and Apple SDKs Agreement and you can do it by running the following in terminal in mac:

sudo xcodebuild -license

After doing that reopen your project.

I encountered the same error for some of my SwiftUI View files when trying to preview on Canvas. What fixed the issue was I copied the code within the current file, deleted the file, created a new SwiftUI View file under the same name and pasted the original code. Hope that helps!

Creating a new folder in Documents/Desktop and copying the files over also resolves the issue.

I bumped into this too, following the Landmarks tutorial. When I created the 'CircleImage.swift' it was not letting me preview it, with the above error message.

You can see the current Scheme you're using by going to Product > Scheme. I've got macOS selected.

Product > Scheme, from there, you can see "Landmarks (macOS)" is selected.

Clicking on the CircleImage.swift file loads it, and in the rightmost sidebar it shows the Attributes inspector. A few buttons to the left of that is "Show the File inspector".

There, you can see this file's chosen "Target Membership". My problem was that "Landmarks (macOS)" was not checked. Checking this immediately got the Preview working.

The File inspector, which looks like a dogearred page, showing the Target Membership list.

I imagine I could also have changed my Product's Scheme to iOS and it would have worked, as that was already checked.

This problem happened to me when I copied a folder into my project with the "Create folder reference" option instead of the "Create groups" option. The problem was solved when I deleted the folder from project and copied the folder again with the latter option.

I just upgraded to both Monterey 12.3, and Xcode 13.3, and boom ran into this issue. Things were fine before the upgrade. Nothing above helped.

Creating a new project does help. So the only thing I found is to create a new project, and just add the files from the old project to the new one. Yuk.

EDIT: I hadn't rebooted the computer (mac mini), after reboot things were fine again.

In the schemes I was able to select, it contained only one scheme. Reopening the folder in a folder that's not in the Downloads directory made live editing work for me

The described issue happened to me after cloning a project via git (no iCloud syncing as described in another answer - so I definitely know that the sync itself was completely done).

Funny enough restarting Xcode did the trick.

Just closing and opening the project isn't enough.

I found that the file I was trying to preview was not listed in Target -> Build Phases -> Compile Sources. Once I manually added the file, Preview worked. I had drag and dropped a directory into the project, and for some reason Xcode had not added those files to that list.

Related