【发布时间】: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