13

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.

guinnessman
  • 433
  • 5
  • 15
  • 1
    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. – cbjeukendrup Aug 29 '19 at 11:15
  • I did try dealing DerivedData previously but it didn't help unfortunately. – guinnessman Aug 29 '19 at 11:20
  • Could you include the code for your `PreviewProvider`? All signs point (at least to me) as though the issue is there. –  Aug 29 '19 at 12:58
  • The PreviewProvider is the default one created by Xcode : ```struct TestView_Previews: PreviewProvider { static var previews: some View { TestView() } }``` – guinnessman Aug 29 '19 at 13:00
  • 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. –  Aug 29 '19 at 15:01
  • Or, simply try renaming **all** your references (and partial references) to `TestView` to `ContentView`? –  Aug 29 '19 at 15:02
  • No I think you have misunderstood. I added a View to the project, naming it TestView, and TestView preview will not work. – guinnessman Aug 29 '19 at 16:11
  • Thanks for the suggestion though. I have now resolved the issue as can be seen in the answer below. – guinnessman Aug 29 '19 at 16:21

4 Answers4

19

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.

guinnessman
  • 433
  • 5
  • 15
  • 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! :-) – ggould75 Sep 24 '19 at 20:53
  • 4
    I was having a regular class called the same as the project. I guess this is a Xcode bug anyway. – Rivera Oct 05 '19 at 13:34
  • 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. – Dylan Reich Nov 07 '19 at 00:03
  • 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?? – guinnessman Nov 08 '19 at 06:30
  • I did the same mistake to name a class with the same name than the app. Renamed. It works for me now. Thanks @guinnessman – Luc-Olivier Feb 25 '20 at 21:54
  • 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`. – cgold Jan 24 '21 at 14:11
5

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.

MarkFromMars
  • 328
  • 2
  • 8
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).

Bret
  • 883
  • 1
  • 9
  • 18
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.

yathavan
  • 2,051
  • 2
  • 18
  • 25