0

I have created a model class from [https://jsonmaster.github.io/#][1] this link in codable, I am getting this kind of error Cannot convert value of type '[[BookingMasterModel]?]' to expected argument type '[BookingMasterModel].Type' Actually I am very new to it please share if there is the solution for it

//MyOrdersModel

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

}

this Booking Master model

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

}
prachit
  • 365
  • 3
  • 10
  • Which line is causing the issue exactly? Also, don't name your var starting with an uppercase. Since your vars in `BookingMasterModel` are optional, you shouldn't need to do the `decodeIfPresent()` for them, in fact you shouldn't need at all that custom `init(from:)`. – Larme Nov 28 '18 at 09:56
  • Unrelated but please conform to the naming convention that variables are **lower**cased and start also with a lowercase letter. And you don't need any CodingKeys if the property names and the keys are the same. The error says that the value for key `BookingMasterModel` is a nested array `[[BookingMasterModel]]` – vadian Nov 28 '18 at 10:04
  • did you manage to fix the issue – anoop4real Apr 11 '19 at 21:52
  • yes ,Robert answer helped me – prachit Apr 13 '19 at 12:31

1 Answers1

1

I would recommend you to use your struct without coding keys because Codable protocol can do this for you.

But your error comes from wrong naming of struct properties. You named your MyOrdersModel property BookingMasterModel same as BookingMasterModel. Replace this variable name with small capital letter.

let bookingMasterModel: [BookingMasterModel]?

also replace let keyword of all properties to var

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

do the same in BookingMasterModel struct.

Now, finally delete all coding keys and inits to make it easier.

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

Note: you can read more about Swift naming convention here

Robert Dresler
  • 10,580
  • 2
  • 22
  • 40