【问题标题】:NSEntityDescription.entity crashes app (SIGABRT)NSEntityDescription.entity 崩溃应用程序(SIGABRT)
【发布时间】:2018-07-17 23:02:02
【问题描述】:

包含NSEntityDescription.entity 的行正在崩溃并给我错误

线程 1:SIGABRT

我看到其他人问过这个问题,推荐的答案是简单地从头开始删除并重新制作实体。我已经这样做了很多次,我还彻底“清理”了代码,并在我的AppDelegate.swift 文件和这个ViewController 文件中导入了CoreData。有人有建议吗?

override func viewDidLoad() {
    super.viewDidLoad()
    addGesture()
    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
    let managedContext = appDelegate.persistentContainer.viewContext
    let stringModelEntity = NSEntityDescription.entity(forEntityName: "StringModels", in: managedContext)!
    let stringBundle = NSManagedObject(entity: stringModelEntity, insertInto: managedContext)
    self.getJSON(stringBundle)
    do {
        try managedContext.save()
    } catch let error as NSError {
        print("Could not save. \(error), \(error.userInfo)")
    }
}

编辑

由于终于打开调试器,我找到了解决方案,以下链接的“最佳答案”描述并解决了这个问题:Core data: Failed to load model

【问题讨论】:

  • 请提供崩溃日志。
  • @matt 没有崩溃日志,它只是崩溃而没有输出到控制台。
  • 好的,但仍有回溯。见stackoverflow.com/questions/7790993/…。 Xcode 为您提供了很多关于 为什么 我们会崩溃的信息。您需要查看该信息以了解发生了什么。
  • 您能否在应用程序的其他地方创建此实体或任何其他实体?我在 Swift Core Data 代码中经常忘记的一件事是将实体的类模块(在数据模型编辑器中)设置为 Current Product Module。
  • 看起来 persistentContainer.storage 在您的调试会话中为零。这与此处相关还是与其他代码有关?也许您将 nil 托管对象上下文发送到 NSEntityDescription.entity?如果是这样,错误似乎在您的 AppDelegate 类中。

标签: ios swift xcode core-data nsentitydescription


【解决方案1】:

这意味着它找不到名为“StringModels”的实体。以我的经验,当程序认为应该存在的东西不存在时,会导致错误 SIGABRT。

我会检查大小写和拼写。

【讨论】:

  • 感谢您的回答。 Model.xcdatamodeld 中的实体被列为“StringModels” - 与上面指定的完全相同。
  • @mahib 当你崩溃时,你能检查你的 'managedContext' 常量在调试区域内是否为 nil 吗?
  • managedContext 为 nil,如上面的屏幕截图所示,我通过将名称“StringModels”(实体名称)更正为“Model”(文件模型的名称)来修复错误。 xcdatamodeld 并包含实体 StringModels。
猜你喜欢
  • 2018-01-09
  • 1970-01-01
  • 1970-01-01
  • 2014-11-08
  • 1970-01-01
  • 2022-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多