Home > OS >  Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.keyNotFou
Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.keyNotFou

Time:11-28

I am getting a weird error and I don't understand why. This hasn't happened prior to adding the new item.

This is the error that I am getting:

Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "hours", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: "hours", intValue: nil) ("hours").", underlyingError: nil))


Here is what I have setup:

Place.Swift:

import SwiftUI
import MapKit

struct Place: Codable, Identifiable {
    
    // MARK: - DEFINE PROPERTIES
    let id: Int
    var b4aId = ""
    var admission: String
    var website: String
    var hours: String
    var show = false
    
    // MARK: - SET DEFAULT PROPERTIES
    static var `default` : Place {
        Place(
            id: 0,
            admission: "Free",
            website: "Website",
            hours: "Hours"
        )
    }
    
    init(
        id: Int,
        admission: String,
        website: String,
        hours: String,
        show: Bool = false
    ) {
        self.id = id
        self.admission = admission
        self.website = website
        self.hours = hours
        self.show = show
    }
    
    init(with p: MGLocation) {
        self.id = atomicId.wrappingIncrementThenLoad(ordering: .relaxed)
        self.b4aId = p.objectId ?? ""
        self.admission = p.admission ?? ""
        self.website = p.website ?? ""
        self.hours = p.hours ?? ""
        self.show = false
    }
}

MGLocation.swift:

import Foundation
import ParseSwift

// MARK: - SET MGLOCATION PARSE OBJECT
struct MGLocation: ParseObject {
    
    // Parse Properties
    var objectId: String?
    var createdAt: Date?
    var updatedAt: Date?
    var originalData: Data?
    var ACL: ParseACL?
    
    // Custom Properties
    var admission: String?
    var website: String?
    var hours: String?
    
    // Initialization
    init() {}
    init(objectId: String?) {
        self.objectId = objectId
    }
}

Notes:

admission and website works just fine, but as soon as I add hours it crashes the app.

The database has the correct column, so I don't know what I am doing wrong compared to the others.

Also, is there an easier way to write the Place class without defining so many instances of keys?

enter image description here

CodePudding user response:

The error message is telling you that there is no hours in at least one entity you are trying to decode. In order to solve this make your hours property optional.

struct Place: Codable, Identifiable {
    
    // MARK: - DEFINE PROPERTIES
    let id: Int
    var b4aId = ""
    var admission: String
    var website: String
    var hours: String? // change this to an optional
    var show = false
    
    // MARK: - SET DEFAULT PROPERTIES
    static var `default` : Place {
        Place(
            id: 0,
            admission: "Free",
            website: "Website",
            hours: "Hours"
        )
    }
    
    init(
        id: Int,
        admission: String,
        website: String,
        hours: String,
        show: Bool = false
    ) {
        self.id = id
        self.admission = admission
        self.website = website
        self.hours = hours
        self.show = show
    }
    
    init(with p: MGLocation) {
        self.id = atomicId.wrappingIncrementThenLoad(ordering: .relaxed)
        self.b4aId = p.objectId ?? ""
        self.admission = p.admission ?? ""
        self.website = p.website ?? ""
        self.hours = p.hours ?? ""
        self.show = false
    }
}
  • Related