swift - 对成员 'decode(_:forKey:)' swift4 的模糊引用
问题描述
我正在解析一个需要从服务器上的字典(这是一种遗留数据格式)转换为简单的客户端字符串数组的响应。因此,我想将名为“数据”的键解码为字典,这样我就可以遍历键并在客户端创建一个字符串数组。
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
do {
let some_data_dictionary = try values.decode([String:Any].self, forKey: CodingKeys.data)
for (kind, values) in some_data_dictionary {
self.data_array.append(kind)
}
} catch {
print("we could not get 'data' as [String:Any] in legacy data \(error.localizedDescription)")
}
}
我得到的错误是:Ambiguous reference to member 'decode(_:forKey:)'
解决方案
看起来 Swift 'Codable' 不能支持 Any 或使用 [String:Any],所以在这里使用这篇文章Swift 4 decodable nested json with random key attributes
我能够为我不会使用的名为 LegacyData 的类创建一个结构,然后将键解压缩到字符串数组中
do
{
let legacy_data = try values.decode([String:LegacyData].self, forKey: CodingKeys.data)
self.array = Array(legacy_data.keys)
}
catch
{
print("no legacy_data \(error) \n")
}
推荐阅读
- mysql - 如何对联合中子查询的操作求和 1 行
- javascript - Vue:绑定来自中间人的多个值
- visual-studio-code - Intellisense 自动完成功能不适用于 vscode 中的 wsl
- python - 将python数据帧转换为字典,然后转换为字符串
- python - 使用for循环将多个数据框列合并为一个
- python - 如何定义 json 的结构以将其转换为 csv
- android - MessagePack 序列化反序列化在两个平台上的结果不同
- java - 反序列化时已经出现 POJO 问题
- python - python:在治疗向量中找不到对照组0级
- php - 当用户未填写字段时如何显示错误?