I am attempting to load race data into my application, and I'm using Loading a specific kind of Codable data as my guide.
I've taken a CSV and converted it to JSON using an online tool.
Here's my struct for holding each individual race result:
struct IndividualResult: Codable, Identifiable {
let place: Int
let name: String
let city: String
let id: Int
let age: Int
let agePlace: Int
let chipTime: String
let gunTime: String
let totalPace: String
}
And my bundle extension to load and decode the JSON. I've modified this to return a simple array of IndividualResult, instead of a dictonary in the example. I wonder if this is part of my problem.
extension Bundle {
func decode(_ file: String) -> [IndividualResult] {
guard let url = self.url(forResource: file, withExtension: nil) else {
fatalError("Failed to locate \(file) in bundle.")
}
guard let data = try? Data(contentsOf: url) else {
fatalError("Failed to load \(file) from bundle.")
}
let decoder = JSONDecoder()
guard let loaded = try? decoder.decode([IndividualResult].self, from: data) else {
fatalError("Failed to decode \(file) from bundle.")
}
return loaded
}
}
Here's my JSON: https://pastebin.com/QXngvmbS
When I run my application and try to load the JSON, I get this error:
Fatal error: Failed to decode results.json from bundle.
I've compared my JSON to my struct and I think they match. Am I misunderstanding that my JSON represents and array? Could the '/' character in the totalPlace string cause an issue?