TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Day 58 - Project 12, part 2 *ISSUE*

Forums > SwiftUI

Hi friends!

The CoreDataProject app keeps crashing on me and provides the following error: Thread 1: "An NSManagedObject of class 'Singer' must have a valid NSEntityDescription."

I've done the Googles and can not figure out how to fix this or what exactly is causing it.

Xcode Version 14.0.1 (14A400) iOS 16 target deploy

2      

When you look at your data model is there an Entity named Singer there?

2      

@Fly0strich,

Yes. It happens when any data model is created. Following along line for line with all topics on Day 57.

2      

SOLVED: As it turns out, I left off the .environment section in the @main part of my app. Easy Fix!!!

import SwiftUI

@main struct CoreDataProjectApp: App { @StateObject private var dataController = DataController() var body: some Scene { WindowGroup { ContentView() .environment(.managedObjectContext, dataController.container.viewContext) } } }

3      

You forgot the backslash \  for: managedObjectContext

\ . managedObjectContext

2      

@KarVarr, it probably wasn't left out but because @leftHandedApps didn't properly format the code block, the forum stripped out the backslash.

@main 
struct CoreDataProjectApp: App { 
    @StateObject private var dataController = DataController() 
    var body: some Scene { 
        WindowGroup { 
            ContentView()
                .environment(\.managedObjectContext, dataController.container.viewContext) 
        } 
    } 
}

vs (with the backslash added back in before I posted):

visual proof

@main struct CoreDataProjectApp: App { @StateObject private var dataController = DataController() var body: some Scene { WindowGroup { ContentView() .environment(.managedObjectContext, dataController.container.viewContext) } } }

Same code, different results. And yet another reason to always properly format code samples.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.