UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 54: Bookworm- An NSManagedObject of class 'Book' must have a valid NSEntityDescription.

Forums > 100 Days of SwiftUI

Hey everyone,

So as I was progressing through day 54, my Bookworm app would crash when I tried to save a book to in it. I would get "An NSManagedObject of class 'Book' must have a valid NSEntityDescription." I hadn't seen anything like this before and this is my first time working with Core Data. I had started a new project, then copied and pasted all the code I had so far and updated the .xcdatamodeld file. While I was doing that, I notices a Persistnace.swift file and also the .xcdatamodeld file had an entity of "Item" within it. Never seen any of the these before and I don't remeber it being brought up in the Paul's tutorial. So after getting my google on, I came to find out that the Persistance.swift file is needed and it had to be updated with the new entity of "Book" with the properties as was set in the Attributes. I had found out this info on this site: https://blckbirds.com/post/core-data-and-swiftui/. Just wanted to put it out there just incase anyone else coming along have the same issue as I did. Maybe this is a new thing the Xcode 12 and iOS 14 update. If we can get this to Paul Hudson so he can update the projects, that would be really dope.

Be blessed, not stressed!

6      

I had solved my own issue, just wanted to bring this to light.

4      

Thanks for the post, fixed my issue too!

3      

Hi, For more convenience i type code here ! :: So you want to create "Persistence.swfit" File

import CoreData

struct PersistenceController {
    static let shared = PersistenceController()

    let container: NSPersistentContainer

    init() {
        container = NSPersistentContainer(name: "bookworm")

        container.loadPersistentStores{ (storeDescription, error) in
            if let error = error as NSError? {
                fatalError("Unresolve Error: \(error)")
            }
        }
    }
}

Bookworm.swift App File :

struct BookwormApp: App {
    let persistenceContainer = PersistenceController.shared // <- so need add this line

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environment(\.managedObjectContext, persistenceContainer.container.viewContext) // <- and here <-
        }
    }
}

Now you can build and Enjoy!😉

5      

Hi everyone 👋 I've got the same error message with the next project (#12).

However, after applying this trick with PersistenceController the app crashes: "Fatal error: UnsafeRawBufferPointer with negative count".

Any ideas how to make it working? Thanks in advance!

5      

@madebyalex

I met the same error, it showed "Fatal error: UnsafeRawBufferPointer with negative count" just like yours.

So I find these codes below :

container = NSPersistentContainer(name: "bookworm")

And I changed the name property with the filename of my xcdatamodeld file,

not the name "bookworm",

but my other name.

Maybe you should check it.

5      

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.