swift - 此类不符合键 Project1 的键值编码。
问题描述
我是iOS开发的新手,你能帮我解决你的错误吗?谢谢你提前。
未捕获的异常“NSUnknownKeyException”,原因:“[setValue:forUndefinedKey:]:此类不符合键 Project1 的键值编码。”
let context = appdelegate.persistentContainer.viewContext
let proj = Project()
let arrProj = dic.object(forKey: "Projects") as! NSArray
for n in 0..<arrProj.count {
let subDic = arrProj.object(at: n) as! NSDictionary
let item = ProjectItem(dict: subDic)
proj.arrProjs.append( item )
let projects = NSEntityDescription.entity(forEntityName: "Projects", in: context)
projects?.setValue(item.Project, forKey: "project1")
projects?.setValue(item.Project2, forKey: "project2")
projects?.setValue(item.ID, forKey: "projectid")
projects?.setValue(item.radius, forKey: "radius")
projects?.setValue(item.GeofenceType, forKey: "geo_Type")
projects?.setValue(item.Geofence, forKey: "geofence")
projects?.setValue(item.Coordinates, forKey: "coordinates")
}
解决方案
您应该在对象中设置值NSManagedObject
,而不是在NSEntityDescription
对象中
let entity = NSEntityDescription.entity(forEntityName: "Projects", in: context)
for n in 0..<arrProj.count {
let projects = NSManagedObject(entity: entity!, insertInto: context)
projects?.setValue(item.Project, forKey: "project1")
projects?.setValue(item.Project2, forKey: "project2")
//...
}
推荐阅读
- javascript - Azure,Graph API 的 Keystone 身份验证
- gatsby - Gatsby 能否与 3rd 方 ui 框架集成
- python - 删除轮廓 OpenCV
- java - MediaPlayer getDataSource 错误权限访问文件
- python - 为什么我的预测模型每次运行时都会给出不同的预测结果?
- python-3.x - 在 sklearn 堆叠中使用 LightGBM 拟合参数
- linux - 我可以使用 Prod Jira 更新 Test Jira
- javascript - OnChange 方法不会更新状态
- c# - GetEntryAssembly 和 GetExecutingAssembly 之间的区别
- excel - 如何将宏/vba 公式应用于特定单元格