简体   繁体   English

类型不符合协议“可解码” /“可编码”

[英]Type does not conform to protocol 'Decodable'/'Encodable'

I have the following data class which I have tried adding an array of AdditionalCharge to but get the error specified in the title. 我有以下数据类,我曾尝试向其中添加AdditionalCharge数组,但得到标题中指定的错误。

The AdditionalCharge class and the FundingRequest class containing the additional charge is below. 下面是AdditionalCharge类和包含额外费用的FundingRequest类。

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
}

Every property in a Codable type must also be Codable as well. Codable类型的每个属性也必须也是可Codable

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, 内置可编码类型 -字符串, 整数 ,双精度型 ,数据,URL数组,

  2. Dictionary, Optional are Codable if they contain Codable types 字典,可选 ,如果包含可编码类型,则为可编码

Hence, the AdditionalCharge must also be Codable for FundingRequest to be Codable . 因此, AdditionalCharge也必须是可Codable以便FundingRequest是可Codable

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

Make AdditionalCharge as Codable 使其他费用可编码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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