This is the first time I have tried to use an API, I get the error "keyNotFound" Could someone point me in the right direction for understanding this better? Here is a link to their documentation: https://developer.ticketmaster.com/products-and-docs/apis/discovery-api/v2/#anchor_find
let apiKey = "************"
struct EventResponse : Decodable{
let events : [Event]
}
struct Event : Decodable{
let id : String?
}
func fetchTMEvents() async throws -> [Event]{
let urlTM = URL(string: "https://app.ticketmaster.com/discovery/v2/events.json?apikey=\(apiKey)")!
let(data, _) = try await URLSession.shared.data(from: urlTM)
let decoded = try JSONDecoder().decode(EventResponse.self, from: data)
return decoded.events
}
Task {
do {
let events = try await fetchTMEvents()
print("API results \(events)")
} catch {
print("API error \(error)")
}
}
Full error:
keyNotFound(CodingKeys(stringValue: "events", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: "events", intValue: nil) ("events").", underlyingError: nil))
Try these test models to fetch then decode the
events. The example code in SwiftUI shows how to display the results.Note, you must have a valid
apiKeyotherwise the response will not be decodable.To get the full set of struct models, just copy the json data (all of it) to https://app.quicktype.io/ and the bulk of the code is generated for you. Adjust that code to suit your needs.
You will have to consult the docs to determine which struct/properties are optional, then add
?to them.