我正在将对象保存在Core Data模型中,但是我发现该密钥与任何密钥都不兼容。
我试图反转键的顺序,但是我要解决的是,我试图更改nome o实体,但是也没有用。
let mediaEntity = NSEntityDescription.entity(forEntityName:
"Media", in: managedContext)
mediaEntity?.setValue(showMediaModel.popularity, forKey: "popularity")
mediaEntity?.setValue(showMediaModel.title, forKey: "title")
mediaEntity?.setValue(showMediaModel.type.rawValue, forKey: "type")
mediaEntity?.setValue(showMediaModel.id, forKey: "id")
do {
try managedContext.save()
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
核心数据模型:
详细的消息错误是:
Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<NSEntityDescription 0x6000008c02c0>
setValue:forUndefinedKey:]: this class is not key value coding-
compliant for the key popularity.'
在启动项目后,我按照以下教程添加了核心数据:https://welcm.uk/blog/adding-core-data-to-an-existing-project-in-xcode-10-swift-4
答案 0 :(得分:0)
检查您的xcdatamodel
文件,并确保“ popularity”的拼写与出现错误的行中的拼写匹配。
答案 1 :(得分:-2)
我忘记创建对象了。
if let mediaEntity = NSEntityDescription.entity(forEntityName: "Media", in: managedContext) {
let newEntity = NSManagedObject(entity: mediaEntity, insertInto: managedContext)
newEntity.setValue(showMediaModel.popularity, forKey: "popularity")
newEntity.setValue(showMediaModel.title, forKey: "title")
newEntity.setValue(showMediaModel.type.rawValue, forKey: "type")
newEntity.setValue(showMediaModel.id, forKey: "id")
do {
try managedContext.save()
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
}