How to get Previews working again in a SwiftUI project
Asked Answered
S

5

14

Recently I have noticed that Previews have stopped working in an app I am working on. I am using the latest Xcode and Catalina (Beta 7). If I add a new View - TestView to my project, its preview fails. This is with no modifications i.e. the default "Hello World" View. The diagnostics say 'TestView' is not a member type of 'MyProject'. Any ideas on how to fix this?

If I create a new project, the Previews work fine.

The project runs fine on an actual device or simulator.

The full diagnostics message is: 'TestView' is not a member type of 'MyProject'


failedToBuildDylib: /Users/nigelhamilton/Library/Developer/Xcode/DerivedData/MyProject-bgufjjqbmfuwcaahjswocfzmwsgx/Build/Intermediates.noindex/Previews/MyProject/Intermediates.noindex/MyProject.build/Debug-iphonesimulator/MyProject.build/Objects-normal/x86_64/TestView.2.preview-thunk.swift:23:32: error: 'TestView' is not a member type of 'MyProject' typealias TestView = MyProject.TestView ~~~~~~~~~ ^ /Users/nigelhamilton/Library/Developer/Xcode/DerivedData/MyProject-bgufjjqbmfuwcaahjswocfzmwsgx/Build/Intermediates.noindex/Previews/MyProject/Intermediates.noindex/MyProject.build/Debug-iphonesimulator/MyProject.build/Objects-normal/x86_64/TestView.2.preview-thunk.swift:16:6: error: replaced accessor for 'body' could not be found @_dynamicReplacement(for: body) private var __preview__body: some View { ^

I am currently deleting code from my project (or at least a duplicate of the project) to see if it eventually starts to show the preview again. I have had two goes at this, and it has eventually worked, but with most of the code deleted. So I am trying to narrow down where the problem might be.

Sacrarium answered 29/8, 2019 at 10:23 Comment(8)
The following might work: - quit Xcode - open Finder, go to folder /Users/nigelhamilton/Library/Developer/Xcode and delete DerivedData - reopen Xcode. This will not cause any data loss, but the next build might take some more time, because Xcode has to do everything again.Adamok
I did try dealing DerivedData previously but it didn't help unfortunately.Sacrarium
Could you include the code for your PreviewProvider? All signs point (at least to me) as though the issue is there.Newbill
The PreviewProvider is the default one created by Xcode : struct TestView_Previews: PreviewProvider { static var previews: some View { TestView() } }Sacrarium
Actually no that isn't the "default" - everything should be named ContentView not TestView. And this is why I'm asking. I'm guessing if you actually go through the pain of (a) creating a brand new project, (b) import everything you've coded and customized without changing a single thing to ContentView everything will build and run. Next, go through the pain of (a) keeping your ContentView but - View by View - include your custom-named things while (b) building and running against all three outputs - device, simulator, Preview - you may find your issue.Newbill
Or, simply try renaming all your references (and partial references) to TestView to ContentView?Newbill
No I think you have misunderstood. I added a View to the project, naming it TestView, and TestView preview will not work.Sacrarium
Thanks for the suggestion though. I have now resolved the issue as can be seen in the answer below.Sacrarium
S
20

I think I have solved this. I had made what probably sounds like a stupid mistake. The project uses CoreData and I had given one of the Entities the same name as the project. Whilst that might sound crazy, the project would build and run (simulator or device) without a problem. It was just the preview that was getting confused. I have renamed the Entity and at least some of the Views can be previewed, including the TestView mentioned above. There are still a couple of Views not previewing correctly, but I think that is due to another issue.

Sacrarium answered 29/8, 2019 at 16:20 Comment(6)
I was also under the impression that the error was due to objects named the same, but I've actually found that you cannot keep more than 1 preview open at the same time. In my project I don't use CoreData but I was testing Watch UI in one window, and iPhone UI in another. Just closing one of the two was making the other one working. I suppose that's a bug of Xcode still in beta, or at least I hope so! :-)Orjonikidze
I was having a regular class called the same as the project. I guess this is a Xcode bug anyway.Dicho
I had the same issue as well. Having a class with the same name as the project seems to break Preview. Is there a bug report to Apple? I wouldn't mind making one.Waterrepellent
I did send a bug report to Apple. They respond with "Please verify this issue with iOS 13.1 beta 3 and update your bug report with your results" which didn't seem very relevant??Sacrarium
I did the same mistake to name a class with the same name than the app. Renamed. It works for me now. Thanks @SacrariumRist
Another example of this is if you have an entity that has the same name as some SwiftUI views, in my case you should not call an entity Group.Personate
P
10

For me I have had good luck addressing this problem using this command:

xcrun simctl --set previews delete all

Appears to do the same thing as an "rm" over the previews simulator, but gets all of the devices and seems to make sure the state of the simulator service is set right afterward.

Possing answered 8/7, 2022 at 17:55 Comment(0)
S
2

In case this is useful to anyone, I fixed the issue of "previews not working" by renaming one of my CoreData model properties from "created" (Double) to "createdTimestamp" (Double).

Scop answered 17/5, 2020 at 0:0 Comment(0)
S
0

Check your simulators, I had an issue preview is blank and showing this message a long time preparing iphone simulator for previews then I manually run the simulator it's not worked I had the error message (Unable to boot the Simulator). After I fixed the simulator issue preview is working.

this solution helped me to resolve the simulator issue.

Simmons answered 5/10, 2022 at 18:33 Comment(0)
F
0

This was the solution for me, thx to @cbjeukendrup in comments:

  1. Quit Xcode
  2. Go to /Users/{user}/Library/Developer/Xcode
  3. Delete DerivedData
  4. Reopen Xcode

Preview specifically hates it when you rename a file.

Furthermost answered 12/3, 2024 at 21:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.