I have a json response which is something like this:
"Details": {
"Attachments": [],
"place": {
"destination": {
"type": "international",
"Id": "superman",
"locationType": "City",
"Name": "Kent"
},
"package": 52.32,
"description": "Dinner"
}
}
}
in this response all the parameters in destination are optional except for type, so i m handling this response like this:
public struct Destination: Decodable, Equatable {
public let Id: String?
public let Name: String?
public let city: String?
public let locationType: String?
public let pinCode: String?
public let country: String?
public let state: String?
public let currency: String?
public let language: String?
public let type: Type
}
as all the parameters are optional and based upto the type i ll be getting only few of these parameters in response.
like –
type: "international",
country: "US"
id: "5",
currency: "Dollar"
is there any way i can write this in an enum:
enum Destination {
case international(country: String, id: String, currency: String)
case national(state: String, language: String, pincode: String)
}
can anyone please answer how should i start with this approach.Thanks
3
Answers
you have to write enum such as
Since Swift 5.5 enums with associated values have Codable synthesis, but this requires the top-level container to contain a single key that matches the name of the enum case. In your case you what a value inside the container to determine the case, so the only solution is to write the
init(from decoder: Decoder)
yourself, decode all the associated values and then return the enum value.Something like this would work:
The above would work with the JSON you have provided: