Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type does not conform to protocol 'Decodable'/'Encodable'

Tags:

swift

I have the following data class which I have tried adding an array of AdditionalCharge to but get the error specified in the title.

The AdditionalCharge class and the FundingRequest class containing the additional charge is below.

I'm not sure what I've done wrong.

class AdditionalCharge{
    var ItemDescription = ""
    var ItemValue:String = ""
}

class FundingRequest: Codable {
let dispatcherName, loadReference, origin, destination: String
let userID: String, applicationID: String
let pickedUp, droppedOff: String
let driverFirstName, driverLastName: String
let grossTruckPay,fundingMethod, comments: String
let advancesTaken: Bool
let rateConfirmation, billOfLading, lumperReceipt: [String]
let salesTicket: [String]
let debtorID: String
let additionalCharges = [AdditionalCharge]()
enum CodingKeys: String, CodingKey {
    case dispatcherName = "DispatcherName"
    case loadReference = "LoadReference"
    case origin = "Origin"
    case destination = "Destination"
    case userID = "UserID"
    case applicationID = "ApplicationID"
    case pickedUp = "PickedUp"
    case droppedOff = "DroppedOff"
    case driverFirstName = "DriverFirstName"
    case driverLastName = "DriverLastName"
    case grossTruckPay = "GrossTruckPay"
    case advancesTaken = "AdvancesTaken"
    case fundingMethod = "FundingMethod"
    case comments = "Comments"
    case additionalCharges = "AdditionalCharges"
    case rateConfirmation = "RateConfirmation"
    case billOfLading = "BillOfLading"
    case lumperReceipt = "LumperReceipt"
    case salesTicket = "SalesTicket"
    case debtorID = "DebtorID"
}

init(dispatcherName: String, loadReference: String, origin: String, destination: String, userID: String,applicationID: String, pickedUp: String, droppedOff: String, driverFirstName: String, driverLastName: String, grossTruckPay: String, advancesTaken: Bool, fundingMethod: String, comments: String, additionalCharges: [AdditionalCharge], rateConfirmation: [String], billOfLading: [String], lumperReceipt: [String], salesTicket: [String], debtorID:String) {
    self.dispatcherName = dispatcherName
    self.loadReference = loadReference
    self.origin = origin
    self.destination = destination
    self.userID = userID
    self.pickedUp = pickedUp
    self.droppedOff = droppedOff
    self.driverFirstName = driverFirstName
    self.driverLastName = driverLastName
    self.grossTruckPay = grossTruckPay
    self.advancesTaken = advancesTaken
    self.fundingMethod = fundingMethod
    self.comments = comments
    self.additionalCharges = additionalCharges
    self.rateConfirmation = rateConfirmation
    self.billOfLading = billOfLading
    self.lumperReceipt = lumperReceipt
    self.salesTicket = salesTicket
    self.debtorID = debtorID
    self.applicationID = applicationID
}
like image 926
Alec. Avatar asked Jan 26 '23 03:01

Alec.


1 Answers

Every property in a Codable type must also be Codable as well.

The simplest way to make a type codable is to declare its properties using types that are already Codable.

  1. Built-in Codable types — String, Int, Double, Data, URL Array,

  2. Dictionary, Optional are Codable if they contain Codable types

Hence, the AdditionalCharge must also be Codable for FundingRequest to be Codable.

class AdditionalCharge: Codable {
    var ItemDescription = ""
    var ItemValue:String = ""
}
like image 89
PGDev Avatar answered Feb 04 '23 20:02

PGDev