I have an API response that is returning a list of objects that are sometimes different and I am having issues trying to decode the list properly. The list will have the object type as the key and then the actual object, it is custom objects types but to save time it looks like this below.
How can I write a custom decode function to decode this response properly?
{
data: [
{
key: "word",
object: "hello"
},
{
key: "number",
object: 15
}
]
}
My code so far:
struct Reponse: Decodable {
let data: [Objects]
}
struct Objects: Decodable {
let key: String
let object: ObjectType
}
enum ObjectType: Decodable {
case word(String)
case numbers(Int)
}
Here is a solution with an added enum for the
keyattribute that is used for determining how to decode theobjectattributeAnd the custom decoding