首页 > 解决方案 > 错误:“此类不符合键值编码” - 对于任何键

问题描述

我在 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

标签: swiftcore-data

解决方案


检查您的xcdatamodel文件并确保“流行”的拼写与给出错误的行中的拼写匹配。


推荐阅读