This is a model design related question related to JSONDecoder codeable functionality on swift.
I have the following JSON:
"author": {
"name": "abc",
"emailAddress": "[email protected]",
"id": 8665,
"displayName": "A B C",
"active": true,
"slug": "abc",
"type": "NORMAL",
"links": {
"self": [
{
"href": "some_href"
}
]
}
}
I am trying to decode this using the new swift 4 Codeable functionality.
Hence I created my struct as follows:
struct Author: Codeable {
let name: String,
let emailAddress: String,
let id: String,
let displayName: String,
let active: String,
let type: String,
let links: [String: [Link]]
}
struct Link: Codeable {
let href: String
}
Once i run a JSONDecoder().decode on this json i get the model objects in the above format. However, the links property in Author class is obtained as a dictionary. And now to access the value of self field i need to roughly do as follows:
let selfLink = author.links["self"]
let href = selfLink[0].href
Is there a better way to model the struct so that this can be avoided?