【问题标题】:Cannot save strings into CoreData无法将字符串保存到 CoreData
【发布时间】:2018-12-08 00:57:35
【问题描述】:

我正在尝试将字符串数组中的字符串保存到核心数据中。我的 .xcdatamodel 如下所示:

我的保存函数(一个名为“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)
    }

最后,在我的 ViewController 中:

Memory().save(from: codes)

但是,我得到的是:

线程 1:致命错误:未解决的错误错误 Domain=NSCocoaErrorDomain Code=134140 "持久化存储迁移 失败,缺少映射模型。” UserInfo={sourceModel=() isEditable 1、实体{

Person = "() 姓名人, managedObjectClassName NSManagedObject, renamingIdentifier Person, isAbstract 0,超实体名称(空),属性 {\n 密码 = \"(), 名字密码, isOptional 1、isTransient 0、实体

【问题讨论】:

    标签: swift core-data xcdatamodel


    【解决方案1】:

    您已更改数据模型,但未能/忘记迁移模型。如果您当前的持久性存储(SQLite 数据库?)中没有任何有价值的东西,那么我建议您将其扔掉并让 Core Data 使用新模型创建一个新的。

    否则,如果您有一个模型并进行适当的迁移,您可能希望从源存储库中获取模型的先前版本。这是感兴趣的SO questionApple's documentation on migration

    【讨论】:

      猜你喜欢
      • 2020-01-02
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      • 2016-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多