Why is an object not being decoded?

前端 未结 1 1433
情书的邮戳
情书的邮戳 2021-01-23 13:53

So I am trying to call backend api to get an object of type horse, I get the object in data but when I try to decode it, it does not work. How can I fix that? You can see my Mes

相关标签:
1条回答
  • 2021-01-23 14:06

    according to your JSON - "type":"GET_HORSE" - you should change enum MessageType : Int,Decodable to enum MessageType: String, Decodable. I've checked your code after changes to a String and got correct model here

    if let json = try? decoder.decode(Message<Horse>.self, from: data!) {
        print(json)
        print("something")
    }
    

    but it's not json, it's a model with Message<Horse> type

    0 讨论(0)
提交回复
热议问题