Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "id", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: "id", intValue: nil) ("id").", underlyingError: nil))
`import Foundation
struct Game: Codable, Identifiable{
var id = UUID()
var title: String
var normalPrice: String
var sellPrice: String
var steamRatingPercent: String
var thumb: String
}
// means data will be read faster in much easier fashion
class API: ObservableObject{
// for updating status of the variable [everytime it updates ]
@Published var games = [Game]()
func loadData(url: String, completion: @escaping ([Game]) -> ()){
guard let url = URL(string: url)
else{
print("Invalid data")
return
}
// decoding JSON data into [Game] array
URLSession.shared.dataTask(with: url){data, response, error in
let games = try! JSONDecoder().decode([Game].self, from: data!)
print(games)
DispatchQueue.main.async{
completion(games)
}
}.resume()
}
}