Understanding and Resolving Preview Pane Error in Xcode Without Restarting: "Active Scheme Does Not Build This File"

188 views Asked by At

I'm working with an Xcode project downloaded from the official Apple tutorial "Drawing Paths and Shapes". I have a feeling the particular project doesn't matter and that this occurs "randomly" but that fact that this is a known-good project is relevant. Anyways, after navigating to the Badge.swift file, the preview pane in Xcode showed an error:

"Cannot do preview because [..].active scheme does not build this file."

I have verified that the project is unmodified from its original state, so typical solutions assuming project configuration issues don't seem applicable. A commonly suggested workaround is to restart Xcode and reopen the file, and this worked, but I'm looking for a more fundamental understanding of what the error actually means and what causes it and a potential resolution that doesn't involve restarting Xcode since there is no "fix" button next to the error.

1

There are 1 answers

0
Sweeper On

I'll fist explain what a scheme is.

You can see the currently selected scheme at the top of the window. Here I selected the scheme "Foo"

enter image description here

The popup that appears when you click on the currently selected scheme shows you all the schemes in your project. Here I only have "Foo".

Different schemes build different targets. Targets are the things listed in your project settings here:

enter image description here

Files can be added to targets on the first tab of the right panel, by checking the checkmarks.

enter image description here

Here, AnotherView.swift does not belong to "Foo" (in fact it doesn't belong to any target), which the currently selected scheme (also called "Foo") builds. Therefore, when I try to add previews to AnotherView.swift, I get the error of "Cannot build preview because the active scheme does not build this file".


That said, the completed sample project here clearly has the schemes correctly set up. The previews don't work probably because of an Xcode bug (which it is known to have lots of :D). I moved the "Complete" folder to my Desktop and the preview now works.

Alternatively, you can download the sample code for the next step, and open the project in the "StartingPoint" folder. Everything in this folder is completely the same as the things in the "Complete" folder of the previous step.