8
votes

I see others post with issues using SwiftUI Previews in Xcode 11 and 12, but have not seen this issue elsewhere.

Older project that worked in Xcode 11, loaded in Xcode 12, fails to launch the preview, no matter how simple the Preview content.

Actual preview code:

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Text("I heart Xcode")
            .previewDevice("iPhone 11")
    }
}

After clicking "Resume" or "Try Again":

CouldNotFindInputNodeInBuiltGraphError<XcodeGraphSource, PreviewInstance>: Preview 1 of "Content View Previews" not found in any targets

Preview 1 of "Content View Previews" must belong to at least one target in the current scheme in order to use previews

Element: PreviewInstance(destinationDescription: UVPreviewPipeline.AnyPreviewDestinationDescription(storage: UVPreviewPipeline.(unknown context at $1382211e0).AnyPreviewDestinationDescriptionBoxImpl<UVIntegration.SimulatorPreviewDestinationDescription>), previewIdentity: UVPreviewPipeline.PreviewIdentity(identifier: ContentView_Previews[0], description: contentType: Element contextType: application device: iPhone 11 index: 0 layout: device supportsLive: true supportsOnDevice: true), variant: nil, variantedIdentifier: ContentView_Previews[0], identifier: AnyHashable(ContentView_Previews[0]))

I only have one target, same as before, with no special configuration. I have tried reloading Xcode as well as copying all sources files to a new directory (saw that one somewhere).

Anyone else able to parse this error message or make a recommendation for how to fix SwiftUI previews?

Thanks.

4
have you tried deleting derived data?byaruhaf
Yes, but good suggestion. Cleaning and option-cleaning build folder as well.Jason
is "PreviewProvider" found in any targets?byaruhaf
After spending a couple of hours restructuring my app from file->new project, carefully testing at each step, preview was working fine until quite suddenly when I wrapped the main view in a NavigationView, the error returned. I undid it, and the error stayed. Rebooting, cleaning build folder, erasing derived data, etc. anything I do it stays. File->new project works again. I think this is an Xcode bug. I even used git to track changes as I went. Weird and frustrating but I think we wait for an update to Xcode to see if it persists.Jason
I filed a bug through Developer Feedback to Apple.Hardy

4 Answers

18
votes

My scenario and resolution is different for the above.

I encountered this error today when I moved all my views into a 'Views' group folder. Then my latest view would not generate a preview and xcode displayed the error reported.

Preview provider "Your View" must belong to at least one target in the current scheme in order to use previews

This error was pointing to an existing view I wasn't currently working on, but it was an opened tab.

I closed all the tabs with views that I had open prior to file restructuring. I then reopen the view I was working on and xcode began redisplaying previews.

5
votes

By chance, I recognized why the previews were broken in my from-scratch-project. Reason was the "Versioning" build phase that I originally added during setup:

xcrun agvtool next-version -all

After wrapping that into a Previews check, everything's fine again:

if [ $ENABLE_PREVIEWS == "NO" ]
then
    xcrun agvtool next-version -all
fi
3
votes

Preview provider "Your View" must belong to at least one target in the current scheme in order to use previews

I had this showing up . This came up when i moved my project to another system by compressing and transfer.

It started working after - i removed the pinned preview . enter image description here

0
votes

My case, I moved to another group folder, but I solved clicking on: file right click > Delete > Remove Reference

Then I added back again to the project: file/folder right click > Add Files to "Project Name"...