Problem while adding Data to Core Data - nil is not a legal parameter

I'm getting this error:

2018-09-08 17:53:36.795035+0200 Movie Roulette[826:263749] [error] error: Failed to load model named ett CoreData: error: Failed to load model named ett 2018-09-08 17:53:36.796536+0200 Movie APP[826:263749] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'MovieData''

Maybe some of you can help me to solve this problem. I have searched and read that I must create an NSManagedObjectContext. I have created this in my func but I'm still getting this error.

func save(name: String) {
    guard let appDelegate =
        UIApplication.shared.delegate as? AppDelegate else {
            return
    }

    // 1
    let managedContext =
        appDelegate.persistentContainer.viewContext
    // 2
    let entity =
        NSEntityDescription.entity(forEntityName: "MovieData",
                                   in: managedContext)!

    let MovieDT = NSManagedObject(entity: entity,
                                  insertInto: managedContext)

    // 3
    MovieDT.setValue(name, forKeyPath: "titel")

    // 4
    do {
        try managedContext.save()
        movieArray.append(MovieDT)
    } catch let error as NSError {
        print("Could not save. \(error), \(error.userInfo)")
    }
}

Upvotes: 0

Views: 228

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

This problem can only happen if the model name changed , here in AppDelegate

let container = NSPersistentcontainer(name:"thisMayChanged")
container.loadPersistentStores......

it's clear in crash

CoreData: error: Failed to load model named ett

and project name is Movie APP

Movie APP[826:263749]

you changed original name to ett as by default it should be Movie_APP

Upvotes: 2

Related Questions