Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Previews working again in a SwiftUI project

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.

like image 531
guinnessman Avatar asked Aug 29 '19 10:08

guinnessman


3 Answers

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.

like image 100
guinnessman Avatar answered Oct 26 '22 20:10

guinnessman


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).

like image 2
Bret Avatar answered Oct 26 '22 21:10

Bret


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.

like image 1
MarkFromMars Avatar answered Oct 26 '22 22:10

MarkFromMars