Below is the JSON file which is meant to be an erray
{
"countries":[
{
"nation":"Eritrea",
"capital":"Asmara",
"population":"6.081 million",
"size":"117,600 km sq"
},
{
"nation":"Somalia",
"capital":"Mogadishu",
"population":"16.360 million",
"size":"637,657 km sq"
},
{
"nation":"France",
"capital":"Paris",
"population":"67.413 million",
"size":"643,801 km sq"
},
{
"nation":"Spain",
"capital":"Madrid",
"population":"47.451",
"size":"505,990 km sq"
}
]
}
However I get an error saying my JSON is a dictionary when i try to pass it in the JSON decoder as shown below:
func load() {
if let fileLocation = Bundle.main.url(forResource: "data", withExtension: "json") {
// do catch in case of an error
do {
let data = try Data(contentsOf: fileLocation)
let jsonDecoder = JSONDecoder()
let dataFromJson = try jsonDecoder.decode([Country].self, from: data)
self.countries = dataFromJson
tableView.reloadData()
} catch {
print(error)
}
}
}
This is the error:
2022-12-17 13:05:01.388603-0500 Milestone Project 13-15[5197:152197] [Assert] UINavigationBar decoded as unlocked for UINavigationController, or navigationBar delegate set up incorrectly. Inconsistent configuration may cause problems. navigationController=<UINavigationController: 0x125017c00>, navigationBar=<UINavigationBar: 0x1247064d0; frame = (0 47; 0 50); opaque = NO; autoresize = W; layer = <CALayer: 0x600003aa6d60>> delegate=0x125017c00
typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))