NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Project 12: An NSManagedObject of class 'Wizard' must have a valid NSEntityDescription.

Forums > SwiftUI


Hey everyone I have been trying to make it runs but I face the same error again


import Foundation
import CoreData

class DataController : ObservableObject {
    let container = NSPersistentContainer(name: "CoreDataProject")

    init() {
        container.loadPersistentStores { description , error in
            if let error = error {
                print("Core Data failed to load: \(error.localizedDescription)")

            self.container.viewContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump


import SwiftUI
import CoreData

struct ContentView: View {

    @Environment(\.managedObjectContext) var moc
    @FetchRequest(sortDescriptors: [], predicate: nil) var wizards: FetchedResults<Wizard>

    var body: some View {
        VStack {
            List(wizards, id: \.self) { wizard in
                Text( ?? "Unknown")

            Button("Add") {
                let wizard = Wizard(context: moc)
       = "Harry Potter"

            Button("Save") {
                do {
                } catch {


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {

DataModel file name is CoreDataProject set Entity to "Wizard", codegen remains Class Definition, constraints is name

I have tried to delete the DataModel file and still failed, and I have tried previous question as reference and still no luck I have tried some methods from stack overflow, anyone has any idea how to fix it?

thanks in advance


Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until October 1st.

Click to save your free spot now

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.