json 致命错误:'try!'运算式意外地引发错误:Swift.解码错误.未找到密钥(编码密钥(字符串值:“小时”,整数值:无)

lnlaulya  于 2022-12-01  发布在  Swift
关注(0)|答案(1)|浏览(81)

我得到了一个奇怪的错误,我不明白为什么。这并没有发生之前,添加新的项目。
这是我得到的错误:
致命错误:'try!'运算式意外地引发错误:Swift.解码错误.未找到密钥(编码密钥(字符串值:“小时”,整数值:nil)、Swift.解码错误.上下文(编码路径:[],调试描述:“没有与关键字CodingKeys相关联的值(stringValue:“小时”,整数值:nil)(“小时”)。",基础错误:无))
以下是我的设置:

地点.斯威夫特

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

备注:
admissionwebsite工作正常,但只要我添加hours,它就会崩溃应用程序。
数据库有正确的列,所以我不知道与其他人相比我做错了什么。
此外,有没有更简单的方法来编写Place类,而不定义这么多的键示例?

nlejzf6q

nlejzf6q1#

错误消息告诉您,至少有一个您尝试编码的实体中没有hours。为了解决这个问题,请将hours属性设为选择性。

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

相关问题