iosswiftcore-dataentityxcdatamodel

+entityForName: could not locate an entity named 'EntityName' in this model


I know this may look as a duplicate but I tried several answers and all of those did not work.

I am trying to use Core Data to store which levels are completed, but I am running in some trouble saving the data. At the moment dat a level is completed the following lines are called.

    let appDelegate =
        UIApplication.shared().delegate as! AppDelegate


    let managedContext = appDelegate.managedObjectContext


    let level = NSEntityDescription.insertNewObject(forEntityName:"SucceededLevels", into:managedContext) as NSManagedObject


    person.setValue(levelName, forKey: "levels")

    do {

        try managedContext.save()

        levelsArray.append(person)
    } catch let error as NSError  {
        print("Could not save \(error), \(error.userInfo)")
    }

But I am getting the following error

"Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an entity named 'SucceededLevels' in this model."

I have set up the entity in the .xcdatamodel.


Solution

  • After days I finally found the problem. The problem was not the code, Xcode 8 Beta was not making a new entity when I clicked on new entity. So I switched to Xcode 7.3, in spite of all the errors with swift 2.3/3.0 and managed to create a new entity.