我收到这个错误:
2018-09-08 17:53:36.795035+0200 Movie Roulette[826:263749] [error] error: Failed to load model named ett CoreData: error: Failed to load model named ett 2018-09-08 17:53:36.796536+0200 Movie APP[826:263749] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'MovieData''
也许你们中的一些人可以帮助我解决这个问题。我已经搜索并阅读到我必须创建一个 NSManagedObjectContext。我已经在我的 func 中创建了它,但我仍然收到此错误。
func save(name: String) {
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}
// 1
let managedContext =
appDelegate.persistentContainer.viewContext
// 2
let entity =
NSEntityDescription.entity(forEntityName: "MovieData",
in: managedContext)!
let MovieDT = NSManagedObject(entity: entity,
insertInto: managedContext)
// 3
MovieDT.setValue(name, forKeyPath: "titel")
// 4
do {
try managedContext.save()
movieArray.append(MovieDT)
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
}
最佳答案
这个问题只有在模型名称更改时才会发生,在 AppDelegate
中
let container = NSPersistentcontainer(name:"thisMayChanged")
container.loadPersistentStores......
崩溃时清晰
CoreData: error: Failed to load model named ett
项目名称为Movie APP
Movie APP[826:263749]
您将原始名称更改为 ett,因为默认情况下它应该是 Movie_APP
关于swift - 将数据添加到核心数据时出现问题 - nil 不是合法参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52236942/