Swift Playgrounds .environmentObject error CreatureZoo()

Can someone please help me figure out how to fix this error message? I’m beginning to think it’s an error in the Playgrounds app since I feel like I have tried everything to fix it.

The error happens when I click the Navigation Link “Make Creatures Dance”.

Any help is much appreciated.

The DancingCreatures View itself works just fine. It crashes when I try to link to DancingCreatures from the main ContentView page.

Thanks,

Swifter20

You have to create the environment object at the top level of your hierarchy and inject it into your topmost view using .environmentObject(yourEnvironmentObject), where yourEnvironmentObject ist the object you created.

See the following article: https://www.hackingwithswift.com/quick-start/swiftui/how-to-use-environmentobject-to-share-data-between-views

Try adding .environmentObject(data) after the navigationStack closure

var body: some Scene { SPCAssessableWindowGroup(app: self, assessmentCandidates: [CreatureZoo()]) { NavigationStack{ ContentView() .navigationTitle("My Creatures") }.environmentObject(data)

For those struggling

Do this:

Try adding .environmentObject(data) after the navigationStack closure

var body: some Scene { SPCAssessableWindowGroup(app: self, assessmentCandidates: [CreatureZoo()]) { NavigationStack{ ContentView() .navigationTitle("My Creatures") }.environmentObject(data)

on the MyApp page

Swift Playgrounds .environmentObject error CreatureZoo()
 
 
Q