Hey y’all… I am creating an airplane tracking app. I get this error of:
keyNotFound(CodingKeys(stringValue: "image", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: \"image\", intValue: nil) (\"image\").", underlyingError: nil))
My view:
import SwiftUI
import SDWebImageSwiftUI
struct ContentView: View {
@State var flight: [FlightModel] = []
var body: some View {
List() {
ForEach(flight) { flight in
Text(flight.airline.name)
Text(flight.aircraft.model)
Text(flight.arrival.airport.name)
Text(flight.departure.airport.name)
Text(flight.status)
}
}
.onAppear {
Api().getFlightData {(flight) in
self.flight = flight
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
My model:
import Foundation
// MARK: - FlightModel
struct FlightModel: Codable, Identifiable {
let id = UUID()
let greatCircleDistance: GreatCircleDistance
let departure, arrival: Arrival
let lastUpdatedUTC, number, callSign, status: String
let codeshareStatus: String
let isCargo: Bool
let aircraft: Aircraft
let airline: Airline
let image: Image
enum CodingKeys: String, CodingKey {
case greatCircleDistance, departure, arrival
case lastUpdatedUTC = "lastUpdatedUtc"
case number, callSign, status, codeshareStatus, isCargo, aircraft, airline
case image = "image"
}
}
// MARK: - Aircraft
struct Aircraft: Codable {
let reg, modeS, model: String
let image: Image
}
// MARK: - Image
struct Image: Codable {
let url: String
let webURL: String
let author, title, imageDescription, license: String
let htmlAttributions: [String]
enum CodingKeys: String, CodingKey {
case url
case webURL = "webUrl"
case author, title
case imageDescription = "description"
case license, htmlAttributions
}
}
// MARK: - Airline
struct Airline: Codable {
let name: String
}
// MARK: - Arrival
struct Arrival: Codable {
let airport: Airport
let scheduledTimeLocal, actualTimeLocal, runwayTimeLocal, scheduledTimeUTC: String
let actualTimeUTC, runwayTimeUTC, terminal: String
let quality: [String]
let checkInDesk, runway: String?
enum CodingKeys: String, CodingKey {
case airport, scheduledTimeLocal, actualTimeLocal, runwayTimeLocal
case scheduledTimeUTC = "scheduledTimeUtc"
case actualTimeUTC = "actualTimeUtc"
case runwayTimeUTC = "runwayTimeUtc"
case terminal, quality, checkInDesk, runway
}
}
// MARK: - Airport
struct Airport: Codable {
let icao, iata, name, shortName: String
let municipalityName: String
let location: Location
let countryCode: String
}
// MARK: - Location
struct Location: Codable {
let lat, lon: Double
}
// MARK: - GreatCircleDistance
struct GreatCircleDistance: Codable {
let meter, km, mile, nm: Double
let feet: Double
}
typealias Welcome = [FlightModel]
class Api {
@State var call = "ANA"
@State var number = "203"
func getFlightData(completion: @escaping([FlightModel]) -> ()) {
guard let url = URL(string: "https://aerodatabox.p.rapidapi.com/flights/number/\(call)\(number)/2021-07-22?rapidapi-key=54c83bd203msh02b59d211459a97p1aab45jsn9e439a9fb505&withAircraftImage=true") else { return }
URLSession.shared.dataTask(with: url) { (data, _, _) in
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
do {
let flight = try decoder.decode([FlightModel].self, from: data!)
DispatchQueue.main.async {
completion(flight)
}
} catch {
print(error)
//add some better error handling here
}
}
.resume()
}
}