Learn Courses My Dashboard

Flight Tracker app error, I don't think this should come out

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
            .onAppear {
            Api().getFlightData {(flight) in
                    self.flight = flight

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {

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 {
                       } catch {
                           //add some better error handling here

Do you have an example of the JSON object?
It’s saying there’s no property with the name “image”

My guess is that Image should be Optional so in the FllightModel struct set that to:

let image: Image?

Also in your Image struct, if you are only using the image url then remove all the other properties as they are just noise. ie

struct Image: Codable {
    let url: String?

There might be other properties in all of the structs that need to be Optional. You probably need more sample data to find out for sure or maybe the API documentation should indicate which are Optional if any.