Reputation: 55
I'm following the https://developer.apple.com/tutorials/swiftui/tutorials for SwiftUI and I've downloaded both macOS Catalina and Xcode 11.0 beta.
Canvas crashed and doesn't restore even after trying the following:
The code simply declares the UI, nothing too fancy.
import SwiftUI
struct LandmarkDetail : View {
var landmark: Landmark
var body: some View {
VStack {
MapView(coordinate: landmark.locationCoordinate)
.edgesIgnoringSafeArea(.top)
.frame(height: 300)
CircleImage(image: landmark.image(forSize: 250))
.offset(y: -130.0)
.padding(.bottom, -130.0)
VStack(alignment: .leading) {
Text(landmark.name)
.font(.title)
.multilineTextAlignment(.center)
HStack {
Text(landmark.park)
.font(.subheadline)
Spacer()
Text(landmark.state)
.font(.subheadline)
}
}
.padding()
Spacer()
}
.navigationBarTitle(Text(landmark.name), displayMode: .inline)
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
LandmarkDetail(landmark: landmarkData[0])
}
}
#endif
I expect the canvas to show the UI, but I keep getting the Cannot preview in this file --- MyApp.app may have crashed
error.
Here is an image of what that looks like:
Please note that everything was working fine until a certain point.
Thanks in advance for your help!
Upvotes: 3
Views: 2774
Reputation: 109
the first crash is related to the enum Category case that was added in Landmark.swift:
enum Category: String, CaseIterable, Codable, Hashable {
case featured = "Featured"
case lakes = "Lakes"
case rivers = "Rivers"
case mountains = "Mountains" . // <- added
}
The second crash that follows is due to resource name changing: If you don't want to update the resources just make sure to rename the yukon_charleyrivers.jpg to 'charleyrivers.jps' or just make sure it matches the .json "imageName" value for the Charley Rivers dictionary.
{
"name": "Charley Rivers",
"category": "Rivers",
"city": "Eaking",
"state": "Alaska",
"id": 1007,
"isFeatured": true,
"isFavorite": false,
"park": "Charley Rivers National Preserve",
"coordinates": {
"longitude": -143.122586,
"latitude": 65.350021
},
"imageName": "charleyrivers", // <- changed from yukon_charleyrivers
}
Upvotes: 1
Reputation: 3195
The above is correct however after updating the JSON to match the resources one from Apple I still had a crash. In this JSON their is also a new category "Mountains" you will have to update the Landmark.swift to include the below case also.
enum Category: String, CaseIterable, Codable, Hashable {
case featured = "Featured"
case lakes = "Lakes"
case rivers = "Rivers"
case mountains = "Mountains"
}
Also make sure you have included all the bundled images (attached for reference). I was missing a couple between the first couple of tutorials from Apple. Seems there are inconsistencies in following along rather than downloading the resources.
Upvotes: 2
Reputation: 4226
I incurred your same error; I fixed by downloading the updated resources from the tutorial. Basically I think you added isFavorite
to the Landmark
model, but in the JSON that field is missing, so it is failing the decode. Download the and replace the new JSON from the tutorial assets.
If the problem is not isFavorite
, should be another resource missing, just be sure to download the new Landmark
model and the associated resources every time you start a new lesson from the tuorial.
This is Apple fault, as they didn't mention that you need to update the JSON file to match the Landmark
model.
EDIT
If you still have problem, just add your json and your Landmark
model here so we can take a look on them
Upvotes: 5