首页 > 解决方案 > NSEntityDescription.entity crashes app (SIGABRT)

问题描述

The line containing NSEntityDescription.entity is crashing and giving me the error

Thread 1: SIGABRT

I have seen other people ask this question, the recommended answer is to simply delete and remake the entity from scratch. I have done this many times, I have also "cleaned" the code thoroughly, and imported CoreData in both my AppDelegate.swift files and this ViewController file. Does anyone have any advice?

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)")
    }
}

Stack Overflow will not let me embed images yet, but here is the Models.xcdatamodeld file.

EDIT

I've found a solution thanks to finally opening the debugger, the following link's 'best answer' describes and solves this issue: Core data: Failed to load model

标签: iosswiftxcodecore-datansentitydescription

解决方案


This will mean that it cannot find the entity with the name "StringModels". In my experience, the error SIGABRT is caused when something that the program thinks should exist does not.

I would check capitalization and spelling.


推荐阅读