How to remove duplicate previews from XCode 12.2?

Viewed 2717

I'm a beginner in XCode and clicked on duplicate preview twice and now I can't find a way to delete the duplicates. I'm sure this is something pretty simple but I've been searching and trying everything for like an hour and can't find a solution for it...

Can somebody help me?

enter image description here

3 Answers

Already found where the problem was! Every time duplicate preview is clicked, it adds one more view in the code.

The code below shows where my "problem" was:

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            ContentView()
            ContentView()
            ContentView()
        }
    }
}

To fix this "problem", simply remove two ContentView() from the code:

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            ContentView()
        }
    }
}

here is the Apple documentation for the above issue it is exactly the same as the answer aforementioned

According to the documentation just delete the ContentView() you can remove the selected instance to preview

    struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    Group {
      ContentView()
    }
  }
}

this is code is from xcode 12.4 so the slightly modify the environment

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
            ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
         
        }
    }
}

I personally believe Apple should think to provides the button on the top same as the other buttons there rather than moving around in the code. it wont cost them a lots. if you try the link above you can notice most of the people having this issue

Looks like you are trying to add multiple Hstacks one after the other. Try addding all Hstacks inside a VStack and your error should solve. Also Make sure you have only one contentView()

Related