swift - 对成员 'decode(_:forKey:)' swift4 的模糊引用

标签 swift swift4 codable

我正在解析需要从服务器上的字典(这是一种遗留数据格式)转换为客户端简单的字符串数组的响应。因此,我想将名为“数据”的键解码为字典,这样我就可以遍历键并在客户端创建一个字符串数组。

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)")
  }
}

我收到的错误是:对成员 '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")
    }

关于swift - 对成员 'decode(_:forKey:)' swift4 的模糊引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50215981/

相关文章:

json - 带有动态键的 Swift Codable

swift - 无法使用类型为 'map' 的参数列表调用 '([AnyObject],(_) -> _)'

ios - layer.addSublayer 与 layer.insertSublayer 动画

ios - 在 swift 中访问状态信息 - DJI Mobile SDK iOS

ios - 由于 RealmSwift,当 swift 3 @obj 推理设置为 "default"时,应用程序崩溃

ios - 无法将类型 '[String : Any]' 的值转换为预期的参数类型 'String'

ios - 将多个选定表格单元格中的数组传递到下一个 View Controller

带有可选数组的 Swift 4 自定义通用结构表现得很奇怪?

swift 4 : Creating an asynchronous serial queue with 2 seconds wait after each job

swift - 如何制作可编码类来解码没有键/名称的 json 数组?