swift - 类与键的键值编码不兼容;变量作为一个集合
问题描述
我一直在尝试使用基于反射的键值编码来保持我的代码灵活和动态,但是我在尝试检索和设置某些属性时遇到了意外问题。
发生的错误是“此类不符合键 [众多键之一] 的键值编码”。相关性类别如下:
class Vote: NSObject & Codable {
var id: String
var userIdentifier: String
var dateTimes: Set<Date>?
var candidates: Set<String>?
var filters: Set<String>?
var locations: Set<String>?
var moods: Set<String>?
var prices: Set<Int>?
var restrictions: Set<String>?
enum CodingKeys: String, CodingKey {
case id = "voteId"
case userIdentifier = "userIdentifier"
case candidates = "candidates"
case dateTimes = "dateTimes"
case filters = "filters"
case locations = "locations"
case moods = "moods"
case prices = "prices"
case restrictions = "restrictions"
}
...
产生错误的行是:
someVoteInstance.value(forKey: "restrictions")
使用 value 和 setValue 的正确方法是什么,这样我可以让我的类既可编码又对 NSObject 函数友好?
解决方案
推荐阅读
- javascript - 如何编辑收到的 JSX 元素以将道具附加到其中的任何 img 元素?
- pandas - 为什么当列中没有 nan 值时,pandas 中的 max() 会返回 nan?
- vue.js - 无法挂载组件:Vuejs 中未定义模板或渲染函数
- reactjs - React Router 的 useParams 导致 useEffect 重新运行
- nativescript - NativeScript 支持自定义 SDK 和蓝牙 LTE
- php - 单击时如何将数据发送到数据库?
- python - 使用 Python 将目录中的 CSV 文件导入 MySql 表
- python - run two luigi WrapperTasks successively
- python - 在唯一列值上对 pandas 应用计数器
- android - 从大型 Android 项目中提取和翻译多个 strings.xml 文件的最佳方法