ios - 无法将类型 '[[ModelName]?]' 的值转换为预期的参数类型 '[ModelName].Type'

标签 ios swift codable decodable encodable

我已经从 [ https://jsonmaster.github.io/#][1] 创建了一个模型类这个链接可编码,我收到这种错误 无法将类型“[[BookingMasterModel]?]”的值转换为预期的参数类型“[BookingMasterModel].Type” 其实我对它很陌生,如果有解决方案请分享

//我的订单模型

import Foundation

struct MyOrdersModel: Codable {

    let BookingMasterModel: [BookingMasterModel]?
    let RESPONSE: String?
    let RESID: String?

    private enum CodingKeys: String, CodingKey {
        case BookingMasterModel = "BookingMasterModel"
        case RESPONSE = "RESPONSE"
        case RESID = "RES_ID"
    }

    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        BookingMasterModel = try values.decodeIfPresent([BookingMasterModel].self, forKey: .BookingMasterModel)
        RESPONSE = try values.decodeIfPresent(String.self, forKey: .RESPONSE)
        RESID = try values.decodeIfPresent(String.self, forKey: .RESID)
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeIfPresent(BookingMasterModel, forKey: .BookingMasterModel)
        try container.encodeIfPresent(RESPONSE, forKey: .RESPONSE)
        try container.encodeIfPresent(RESID, forKey: .RESID)
    }

}

这个 Booking Master 模型

import Foundation

struct BookingMasterModel: Codable {

    let AMOUNT: String?
    let APPTDATE: String?
    let CANCELLEDON: String?
    let COUNT: String?
    let DATE: String?
    let MOBILE: String?
    let NAMES: String?
    let ORDERBY: String?
    let ORDERMODE: String?
    let ORDERNO: String?
    let REMINDER: String?
    let SERVICEDON: String?
    let STATUS: String?

    private enum CodingKeys: String, CodingKey {
        case AMOUNT = "AMOUNT"
        case APPTDATE = "APPT_DATE"
        case CANCELLEDON = "CANCELLED_ON"
        case COUNT = "COUNT"
        case DATE = "DATE"
        case MOBILE = "MOBILE"
        case NAMES = "NAMES"
        case ORDERBY = "ORDER_BY"
        case ORDERMODE = "ORDER_MODE"
        case ORDERNO = "ORDER_NO"
        case REMINDER = "REMINDER"
        case SERVICEDON = "SERVICED_ON"
        case STATUS = "STATUS"
    }

    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        AMOUNT = try values.decodeIfPresent(String.self, forKey: .AMOUNT)
        APPTDATE = try values.decodeIfPresent(String.self, forKey: .APPTDATE)
        CANCELLEDON = try values.decodeIfPresent(String.self, forKey: .CANCELLEDON)
        COUNT = try values.decodeIfPresent(String.self, forKey: .COUNT)
        DATE = try values.decodeIfPresent(String.self, forKey: .DATE)
        MOBILE = try values.decodeIfPresent(String.self, forKey: .MOBILE)
        NAMES = try values.decodeIfPresent(String.self, forKey: .NAMES)
        ORDERBY = try values.decodeIfPresent(String.self, forKey: .ORDERBY)
        ORDERMODE = try values.decodeIfPresent(String.self, forKey: .ORDERMODE)
        ORDERNO = try values.decodeIfPresent(String.self, forKey: .ORDERNO)
        REMINDER = try values.decodeIfPresent(String.self, forKey: .REMINDER)
        SERVICEDON = try values.decodeIfPresent(String.self, forKey: .SERVICEDON)
        STATUS = try values.decodeIfPresent(String.self, forKey: .STATUS)
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeIfPresent(AMOUNT, forKey: .AMOUNT)
        try container.encodeIfPresent(APPTDATE, forKey: .APPTDATE)
        try container.encodeIfPresent(CANCELLEDON, forKey: .CANCELLEDON)
        try container.encodeIfPresent(COUNT, forKey: .COUNT)
        try container.encodeIfPresent(DATE, forKey: .DATE)
        try container.encodeIfPresent(MOBILE, forKey: .MOBILE)
        try container.encodeIfPresent(NAMES, forKey: .NAMES)
        try container.encodeIfPresent(ORDERBY, forKey: .ORDERBY)
        try container.encodeIfPresent(ORDERMODE, forKey: .ORDERMODE)
        try container.encodeIfPresent(ORDERNO, forKey: .ORDERNO)
        try container.encodeIfPresent(REMINDER, forKey: .REMINDER)
        try container.encodeIfPresent(SERVICEDON, forKey: .SERVICEDON)
        try container.encodeIfPresent(STATUS, forKey: .STATUS)
    }

}

最佳答案

我建议您在不对 key 进行编码的情况下使用您的结构,因为 Codable 协议(protocol)可以为您做到这一点。

但是你的错误来自结构属性的错误命名。您将 MyOrdersModel 属性命名为 BookingMasterModelBookingMasterModel 相同。将此变量名替换为小写大写字母。

let bookingMasterModel: [BookingMasterModel]?

还将所有属性的let关键字替换为var

var bookingMasterModel: [BookingMasterModel]?
var RESPONSE: String?
var RESID: String?

BookingMasterModel 结构中做同样的事情。

现在,最后删除所有编码键和初始化以使其更容易。

struct MyOrdersModel: Codable {

    var bookingMasterModel: [BookingMasterModel]?
    var RESPONSE: String?
    var RESID: String?

}

struct BookingMasterModel: Codable {

    var AMOUNT: String?
    var APPTDATE: String?
    var CANCELLEDON: String?
    var COUNT: String?
    var DATE: String?
    var MOBILE: String?
    var NAMES: String?
    var ORDERBY: String?
    var ORDERMODE: String?
    var ORDERNO: String?
    var REMINDER: String?
    var SERVICEDON: String?
    var STATUS: String?
}

注意:您可以阅读有关 Swift 命名约定的更多信息 here

关于ios - 无法将类型 '[[ModelName]?]' 的值转换为预期的参数类型 '[ModelName].Type',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53516513/

相关文章:

swift - 如何从可编码结构中的空数组中删除双引号

ios - 使用 Codable 序列化为 JSON 时 Swift String 转义

iphone - 为什么我的触摸坐标无法在屏幕上正确映射?

ios - CURRENT_ARCH 扩展不正确?

ios - 在 adblocker 中收到错误 ContentBlockerErrorDomain Code=3

swift - 在 Swift 中使用 Codable 解析 JSON 数据

ios - NSSortDescriptor 问题

ios - Swift - 本地通知不会被触发

swift - 无法构建 objective-c 模块 'FBSDKCoreKit'

ios - 如何将 SDWebImage 转换为 UIImage?