简体   繁体   中英

Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "hours", intValue: nil)

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?

在此处输入图像描述

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM