Reputation: 1395
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.
Upvotes: 45
Views: 36529
Reputation: 71
For the sake of helping someone else out that make the mistake I did. I accidentally deleted the 'Compile Sources' in the 'Build Phases' of my project's Target. I then had to re-add all the files that needed compiling in the scheme to get the preview working again. Click on the "+" in the top left and choose 'New Compile Sources Phase', then in the new section created , tap that "+" and choose all the files in your project that need compiling.
Upvotes: 2
Reputation: 31
Restarting the Xcode work for me.
My case: Clone and running swiftUI project in another device.
Upvotes: 0
Reputation: 382
There are 9k reasons of this error. My case: I'm developing Swift Package. When I added Example folder to this Package and put Demo project inside - My Xcode Canvas (#Preview {}) displayed error above. So, I solved it by closing Xcode window with my Swift Package.
Upvotes: 0
Reputation: 17596
I ran into this error when the path to my Swift project contained hyphens (ex. ~/Dev/VisionOS-tips-and-tricks/Day001).
Changing the folder name to something without hyphens seemed to fix the issue (ex. ~/Dev/VisionOSTips/Day001).
Upvotes: 1
Reputation: 2068
For me, I go to system settings -> storage -> Developer -> delete "Project Build data and indexes" to fix this problem.
Upvotes: 0
Reputation: 2811
I just changed my preview code from this format:
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
To this format:
#Preview {
ContentView()
}
Everything else failed.
Upvotes: 0
Reputation: 3455
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.
Upvotes: 1
Reputation: 17
Another solution:
Make sure you select file type Swift UI instead of Swift File when you create the file.
Upvotes: -1
Reputation: 344
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.
Upvotes: 1
Reputation: 1
Try About this mac -> Storage -> Manage -> Developer, Then delete Xcode cache from here then restart Xcode...This worked out for me
Upvotes: 0
Reputation: 105
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.
Upvotes: 0
Reputation: 2344
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.
Upvotes: 1
Reputation: 3999
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.
Upvotes: 2
Reputation: 1781
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.
Upvotes: 30
Reputation: 1075
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.
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.
I imagine I could also have changed my Product's Scheme to iOS and it would have worked, as that was already checked.
Upvotes: 5
Reputation: 71
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.
Upvotes: 5
Reputation: 1
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
Upvotes: 0
Reputation: 539
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.
Upvotes: 43
Reputation: 474
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.
Upvotes: 4
Reputation: 61
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!!!
Upvotes: 6