0

I'm trying to save strings I have in my string array into the Core Data. My .xcdatamodel looks like this:

.xcdatamodel

My saving function (a method of a class called "Memory"):

func save(from: [String])
    {
        for i in 0..<from.count
        {
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            let context = appDelegate.persistentContainer.viewContext

            let saved = NSEntityDescription.insertNewObject(forEntityName: "Person", into: context)

            saved.setValue(from[i], forKey: "password")

            do
            {
                try context.save()
                print("SAVED")
            }

            catch
            {
                print("ERROR - COULDN'T SAVE ", to)
            }
        }

        print("NEW ", to, ": ")
        print(save)
    }

Lastly, inside my ViewController:

Memory().save(from: codes)

However, what I get is this:

Thread 1: Fatal error: Unresolved error Error Domain=NSCocoaErrorDomain Code=134140 "Persistent store migration failed, missing mapping model." UserInfo={sourceModel=() isEditable 1, entities {

Person = "() name Person, managedObjectClassName NSManagedObject, renamingIdentifier Person, isAbstract 0, superentity name (null), properties {\n password = \"(), name password, isOptional 1, isTransient 0, entity

Zander
  • 84
  • 14
aleksy.t
  • 267
  • 2
  • 18

1 Answers1

0

You have made changes to your data model but you failed/forgot to migrate the model. If you don't have anything valuable in your current persistence store (SQLite database?) then I suggest you throw it away and let Core Data create a new one using the new model.

Otherwise you might want to fetch the previous version of your model from your source repository if you have one and do a proper migration. Here is a SO question of interest and Apple's documentation on migration

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52