WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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!


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


Thanks for the post, fixed my issue too!


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 {
                .environment(\.managedObjectContext, persistenceContainer.container.viewContext) // <- and here <-

Now you can build and Enjoy!😉


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!



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.


Hacking with Swift is sponsored by RevenueCat

SPONSORED You know StoreKit, but you don’t want to do StoreKit. RevenueCat makes it easy to deploy, manage, and analyze in-app subscriptions on iOS and Android so you can focus on building your app.

Explore the docs

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

Reply to this topic…

You need to create an account or log in to reply.

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.