【问题标题】:Getting Error while Using JSONDecodable in swift4?在 swift 4 中使用可编码的 JSON 时出错?
【发布时间】:2018-04-08 21:51:30
【问题描述】:

在 swift4 中使用 JSONDecodable 时出现以下错误。 "Type UserRegister does not confirm to protocol 'Decodable' "

我的模型结构如下所示。

struct UserRegister: Decodable {
    let id : NSInteger
    let name : String?
    let email : String?
    let cities : Array<Any>?
    let tax : [tax]
    let username : [username]
}

struct tax : Decodable {
    let deviceId : NSInteger?
    let financialYear : String?
    let country : String?
    let name : String?
    let state : String?
    let taxCode : String?
    let value : NSInteger? 
}

struct username : Decodable {
    let email : String?
    let phone : String?
}

【问题讨论】:

  • 您的代码中没有 xxxxx 类型,所以我怀疑错误是否说明了您所说的内容。
  • 现在编辑问题。
  • 另外请以大写字母开头的类型名称,例如taxusername:Tax, Username。

标签: ios json swift swift4 jsondecoder


【解决方案1】:

问题是Array&lt;Any&gt;。您需要一个 Decodable 类型的数组,例如 String 或 Int。

【讨论】:

  • 试过这个,但仍然得到同样的错误,Converted NSInteger to Int.
  • @matt + @Zac24 还有Array&lt;Any&gt;
  • @staticVoidMan 是的,事实证明仅此而已。
  • 是的,这解决了它。
  • 我知道我们不同意是否结束投票回答问题 - 但是(假设结束投票是你的):如果 OP 编辑​​了问题并且你回答它,那你为什么不收回最后一票呢?
猜你喜欢
  • 1970-01-01
  • 2019-04-26
  • 2018-07-30
  • 2018-08-14
  • 1970-01-01
  • 1970-01-01
  • 2019-01-11
  • 2018-11-18
  • 2018-12-03
相关资源
最近更新 更多