39

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.

ruhulrahat
  • 1,325
  • 4
  • 15
  • 23
  • 4
    I faced a lot of the same issues. Are you running it from the downloads folder? this seemed to cause me issues (few comments about it online). and you can try a deep clean by `ctrl+alt+shit + k` and then build the app again. – Simon McLoughlin Oct 16 '19 at 15:30
  • Copy and paste project from download folder to desktop worked for me... – Tianyun Ling Jan 15 '20 at 15:24

16 Answers16

37

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.

Daniel Elkington
  • 2,560
  • 6
  • 22
  • 35
Alain ANDRE
  • 471
  • 1
  • 4
  • 3
  • 1
    Well that fixes it for me. Is there another way to diagnose this other than "unplug the computer and restart?" Surely files syncing should not continue being an issue for hours after any syncing stopped? What is the underlying error here? is this just a dumb way to do the "ctrl+alt+shit + k" from above? – Dirk Bester Apr 06 '20 at 23:11
  • This fix worked for me, though the cause was different in my case: I’d (1) opened an xcodeproj, then (2) run pod init and (3) opened the xcworkspace instead. Switching from project to workspace apparently confused Xcode. Quitting & reopening Xcode fixed the issue. – Paul Cantrell Jul 31 '21 at 05:09
19

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.

Keivan
  • 1,300
  • 1
  • 16
  • 29
  • 4
    At first I thought, "This won't work. What does this have to do with the error?" But, sure enough, it did work. WHY? – Mark Moeykens Feb 10 '22 at 19:10
  • It is the way of accepting the conditions and license agreement that have been mentioned [here](https://www.apple.com/legal/sla/docs/xcode.pdf) such as using location services, HealthKit APIs, etc. in your App. – Keivan Feb 11 '22 at 12:23
  • Unbelievable, this does work! My and my colleagues previews have been broken since we installed Xcode 13 beta. I've searched for months off and on trying to figure this out. Certainly Xcode could be smart enough to tell us this... – David Hunt May 01 '22 at 23:15
  • LOL worked for me after trying all the other options with "Target Membership" and thinking maybe its a different file having issues. Amazing – Beau Nouvelle Aug 30 '22 at 03:22
  • WHY DOES THIS WORK?????????? MY god man – Steven-Carrot Oct 11 '22 at 00:48
  • this is the most facepalm solution I've ever read – Steven-Carrot Oct 11 '22 at 00:49
17

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

ILYA2606
  • 587
  • 3
  • 7
6

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

5

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.

Shane
  • 1,015
  • 2
  • 12
  • 31
  • 1
    I just had the same issue with the Garden App example for Swift UI on macOS. It's not that the file is added to the scheme or not, but that the project defaults to the 'Garden App End' scheme on loading and not the 'Garden App Start' scheme when doing the code along. Hope this helps somebody else :) – maninvan Jul 31 '21 at 21:45
4

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.

Wayne Ohm
  • 61
  • 1
3

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.

JasonY
  • 341
  • 1
  • 6
2

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.

ubuntudroid
  • 3,680
  • 6
  • 36
  • 60
1

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.

Jack
  • 2,206
  • 3
  • 18
  • 25
0

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

0

Restarting my PC worked for me.

cigien
  • 57,834
  • 11
  • 73
  • 112
Eray Hamurlu
  • 657
  • 7
  • 9
0

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.

0

Try About this mac -> Storage -> Manage -> Developer, Then delete Xcode cache from here then restart Xcode...This worked out for me

Bravman
  • 1
  • 2
0

Make sure the file you are trying to preview is in your app (the folder with the same name as your project).

I was able to fix newly created files not previewing by moving the file into my project.

FBB
  • 326
  • 1
  • 12
0

In my case I cleaned the derivedDate folder and restart Xcode solves the issue.

Update 2023: I reopen the project with Xcode 14.3 and the issue is solved.

Bill Chan
  • 3,199
  • 36
  • 32
-1

Another solution:

Make sure you select file type Swift UI instead of Swift Fileenter image description here when you create the file.

puma-lima
  • 17
  • 3