更改Coredata中的属性给我NScocoaErrordomain代码= 134140

发布于 2025-02-05 20:08:43 字数 1323 浏览 0 评论 0原文

中使用Xcode 13.4和Swift 5有一个映射模型问题

我在Coredata

street_no    String

。更改它,删除了所有两个类文件,再生代码,我的应用程序总是会随下错误崩溃:

[错误]错误: AddPersistentStoreWithType:配置:URL:选项:错误:返回 错误nscocoaerrordomain(134140)

AppDelegate函数中的

lazy var persistentContainer: NSPersistentContainer = {
        let container = NSPersistentContainer(name: “Invoice_Gen")
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {

                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        return container
    }()

如果我将属性更改为字符串,则一切正常。

在大量谷歌搜索后,我发现的长时间解决方案是:

  1. 保持属性, sweet_no, ,我想更改它最初定义(字符串)
  2. 创建一个称为 sweet_no_2 的新属性,并将其定义为INT64
  3. 的实体的两个类文件
  4. 删除包含属性再生 从编辑器菜单
  5. 清洁项目文件夹
  6. 构建&运行
  7. 删除原始属性, street_no
  8. 删除包含属性的实体的两个类文件,
  9. 从编辑器菜单
  10. 清洁项目文件夹
  11. 构建&运行
  12. 重命名 street_no_2 street_no
  13. 删除包含属性的实体的两个类文件,
  14. ental属性重新生成实体的类文件
  15. 从编辑器菜单清洁项目文件夹
  16. 构建&amp and&amp and and 。在我确定上面的解决方案可以缩短时运行

,显然问题与映射模型有关。

当然,必须有一种方法可以更改属性的类型?

I have a mapping model problem in CoreData with Xcode 13.4 and Swift 5.

Originally when I created the entities in CoreData, one of them had an attribute that was defined as

street_no    String

Once I realized that I meant to define it as an Int64, I went and changed it, deleted all the two class files, regenerated the code, and my app would always crash with the following error:

[error] error:
addPersistentStoreWithType:configuration:URL:options:error: returned
error NSCocoaErrorDomain (134140)

in the AppDelegate function:

lazy var persistentContainer: NSPersistentContainer = {
        let container = NSPersistentContainer(name: “Invoice_Gen")
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {

                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        return container
    }()

If I changed the attribute back to String, everything worked fine.

After much Googling, the long winded solution I found was:

  1. Keep the attribute, street_no, I want to change as it was originally defined (string)
  2. Create a new attribute called, street_no_2, and define it as an Int64
  3. Delete the two class files for the entity containing the attribute
  4. Regenerate the class files for the entity manually from the Editor menu
  5. Clean Project Folder
  6. Build & Run
  7. Delete the original attribute, street_no
  8. Delete the two class files for the entity containing the attribute
  9. Regenerate the class files for the entity manually from the Editor menu
  10. Clean Project Folder
  11. Build & Run
  12. Rename street_no_2 to street_no
  13. Delete the two class files for the entity containing the attribute
  14. Regenerate the class files for the entity manually from the Editor menu
  15. Clean Project Folder
  16. Build & Run

While I am sure the solution above can be shortened, obviously the problem has to do with the mapping model.

Surely there must be a way to just change an attribute's type?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停顿的约定 2025-02-12 20:08:44

由于存储的数据库文件之间的不匹配,您有一个字符串以及所告诉核心数据的内容,因此出现了错误,现在是INT。您指的是映射模型问题,但听起来似乎根本没有使用过映射模型,只是直接更改了模型定义,并期望框架应付。

如果这是一个早期阶段应用程序(即您仍在开发第一个版本),只需从设备或模拟器中删除该应用程序,然后重新运行模型和类定义文件。

如果您的应用程序在野外和用户设备上,并且您不在乎保留数据,则需要根据您定义的某些默认键在启动上包含一些代码来删除数据库文件。

如果要保留数据,则需要在核心数据模型编辑器中创建一个新模型版本,其中定义了您喜欢的类型,然后添加迁移或映射规则以定义转换期间应该发生什么 - 我认为没有任何自动缩小的映射规则将任何字符串变成整数。

The error comes because of a mismatch between the stored database file, where you have a string, and what you've told Core Data is in there, which is now an Int. You refer to a mapping model problem but it doesn't sound like you've used a mapping model at all, just changed the model definition directly and expected the framework to cope.

If this is an early stage app (i.e you're still developing the first version) just delete the app from the device or simulator and re-run once you've changed the model and class definition files.

If your app is out in the wild and on user devices, and you don't care about keeping the data, you'll need to include some code to delete the database file on startup according to some defaults key that you define.

If you want to keep the data, then you need to create a new model version in the core data model editor, where the type is defined how you like it, and then add migration or mapping rules to define what should happen during the conversion - I don't think there is any automatically inferrable mapping rule to turn any string into an integer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文