Had the same issue. Are you by any chance doing the tutorial on a MacBookPro?
The Swift UI tutorials experience of right panel code example changing and highlighting changes as you scroll the instructions on the left is innovative and generally works, but has some drawbacks. One is that if the code example on the right is too big to fit in the window, there's no way to know that. Following convention since 10.7 Lion, the scrollviews are hidden until you start to scroll.
So when you get to "Handling User Input" Section 5 Step 5, there's no good way to know that if on a MacBookPro 15" Safari at default resolution and size, apparantly, you need to scroll down and see additional changes that you need to make to the "PreviewProvider" in order for the preview to work.
That the error message blames your app -- your app "may have crashed" is just user-hostile. The app builds and runs, it's the preview that has crashed, and astonishing they can't surface that information in the UI or crash reports but defualt to text blaiming the app. I only found it by running the tutorial that was crashing and the "complete" project provided through diff tool FileMerge.
But that no one did the tutorials on a MacBookPro 15" to see this problem is just come on.
After looking again, a few points.
First, If the EnivronmentObject is not provided, the code has crashed, so in that sense the warning is accurate.
However, it's misleading to say "YourApp.app" may have crashed, since the preview does not go through the app's normal control flow. It starts at the PreviewProvider. Since the PreviewProvider is a concrete type, it would be better to say `LandmarkList_Previews has crashed` or just otherwise name-check the specific PreviewProvider (instead of "Cannot preview in this file" so that we can know to start looking in the preview.
Really, best case would be the missing EnivronmentObject should be detectable at compile time, though I don't know how that would work. Notably, the crash happens even if the EnvironmentObject is not used anywhere in the body. Also, when running the actual app target, if the EnivronmentObject is not set on the rootView of the UIHostingController that is the window's rootViewController, then the crash happens on makeKeyAndVisible; anywhere else, the crash just goes to UIApplicationMain. Regardless, a missing EnvironmentObject object at runtime should be able to determe that and trap with that message, not fail on an error with StoreBox.
Hi, I'm on the Handling User Input tutorial, and I always fail section 5(https://developer.apple.com/tutorials/swiftui/handling-user-input). I followed each step meticulously and after I did section 5 step 1, Xcode popped up an error "Use of undeclared type 'UserData'". I tried to ignore and followed through. However, my LandmarkList, LandmarkDetail, SceneDelegate files warned me of the same error. I even checked the completed version of the project that I downloaded from the tutorial page. They looked exactly the same except the parts that I haven't done. Do everyone here have similar experience as mine or can kindly offer some suggestions? Thank you!
I had this problem.
I *think* that I fixed it, by altering the bundle identifier from the default apple identifier, and making sure that I was using my team that has an active developer account.
However on a late 2016 MacBook Pro 13" with touchbar, the whole codebase for those downloaded tutorials is so buggy that I'm constantly having to clean, rebuild and manually delete derived data files to get the app and preview to work, so I can't be entirely sure that any one action is what fixed it.
I hope this is helpful.
I've encountered the exact same issue. The app will build and I can run it in Simulator, but I cannot get rid of the "cannot preview in this file" error that this page discusses. I filed a bug report and I recommend others do the same.
Adding this line to Landmark_ListPreviews got it working for me:
I am facing the same issue too, and here is my solution
If you use the code from "Building Lists and Navigation" tutorial, you need to modified landmarkData.json and Landmark.swift file to solve the problem
In landmarkData.json file, you need to add "isFavorite":true or "isFavorite":false property to every data, and then add variable "isFavorite : Bool" in Landmark.swift.
Hey everyone - Had the same thing as above ... but this solution worked for me: Cleaning the DerivedData out ... essentially clearing the compiled cache of xCode build temps : https://stackoverflow.com/questions/46468220/how-can-i-delete-derived-data-in-xcode-9/46468462#46468462
This solution seemed so valuable that I aliased the DerivedData directory from my ~/Developer
I found this solution from https://forums.developer.apple.com/thread/120960
Hope this helps