‘Keep going with apps’ Crash during Creature Add steps

Hello fellow Techies!

I am currently doing the Swift Playgrounds “Keep Going with Apps” on my iPad Pro. Everything has been going as designed until I got to the ‘Add and Delete Creatures‘ module. The lesson concludes with being able to add a creature to your list in the CreatureZoo. When you run the app from the playground, you can fill out the fields and tap ‘Add’ and that is when the app dims and after 5 seconds I get the notification that the app has an unknown crash. I have reviewed the lessons leading up to it, but I can’t find anything wrong. (Full Disclosure: I really have no clue what I am looking at some of time.)

Has anyone else ran into this?

`import SwiftUI import Guide

struct CreatureEditor: View { //#-learning-code-snippet(defineVariablesCreatureEditor) //#-learning-code-snippet(environmentValue) @State var newCreature : Creature = Creature(name: "", emoji: "") @EnvironmentObject var data : CreatureZoo @Environment(.dismiss) var dismiss var body: some View { SPCAssessableGroup(view: self) { VStack(alignment: .leading) { Form { Section("Name") { //#-learning-code-snippet(addACreatureEditorTextField) TextField("What is your monster's name?", text: $newCreature.name) }

Section("Emoji") {
TextField("What does your monster look like?", text: $newCreature.emoji)
}
Section("Creature Preview") {
CreatureRow(creature: newCreature)
}
}
}
.toolbar {
ToolbarItem {
Button("Add") {
data.creatures.append(newCreature)
dismiss()
}
}
}
//#-learning-code-snippet(addButtonToToolbar)
}
}

}

struct CreatureEditor_Previews: PreviewProvider { static var previews: some View { NavigationStack() { CreatureEditor().environmentObject(CreatureZoo()) } } }’

Hello! Looks like you're missing a backslash character on one of your lines of code.

@Environment(.dismiss) 

should be

@Environment(\.dismiss) 

That seems to resolve the crash on our end. Hope this helps!

import Guide
struct CreatureEditor: View {
//#-learning-code-snippet(defineVariablesCreatureEditor)
//#-learning-code-snippet(environmentValue)
@State var newCreature : Creature = Creature(name: "", emoji: "")
@EnvironmentObject var data : CreatureZoo
@Environment(\.dismiss) var dismiss
var body: some View {
SPCAssessableGroup(view: self) {
VStack(alignment: .leading) {
Form {
Section("Name") {
//#-learning-code-snippet(addACreatureEditorTextField)
TextField("What is your monster's name?", text: $newCreature.name)
}
Section("Emoji") {
TextField("What does your monster look like?", text: $newCreature.emoji)
}
Section("Creature Preview") {
CreatureRow(creature: newCreature)
}
}
}
.toolbar {
ToolbarItem {
Button("Add") {
data.creatures.append(newCreature)
dismiss()
}
}
}
//#-learning-code-snippet(addButtonToToolbar)
}
}
}
struct CreatureEditor_Previews: PreviewProvider {
static var previews: some View {
NavigationStack() {
CreatureEditor().environmentObject(CreatureZoo())
}
}
}

Here is the code snippet fully. Sorry I wasn't able to post it before - I didn't know how. I still can't get it to work after I click add.

Is there a way to post the whole codebase?

I am happy to take another look to see what is going on. Just to make sure I am understanding the situation correctly, you're on the ‘Add and Delete Creatures‘ task, and one of the code snippets is not being added to the source editor. Is that correct? Could you point out which code snippet isn't being added?

‘Keep going with apps’ Crash during Creature Add steps
 
 
Q