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

时间:2019-06-28 13:30:03

标签: swift

我有以下数据类,我曾尝试向其中添加AdditionalCharge数组,但得到标题中指定的错误。

AdditionalCharge类和包含额外费用的FundingRequest类在下面。

我不确定我做错了什么。

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
}

2 个答案:

答案 0 :(得分:4)

Codable类型的每个属性 也必须为Codable

  

使类型可编码的最简单方法是声明其属性   使用已经可编码的类型。

     
      
  1. 内置可编码类型 —字符串,整数,双精度,数据,URL数组

  2.   
  3. 词典,可选,如果包含可编码类型,则为可编码

  4.   

因此,AdditionalCharge的{​​{1}}也必须为Codable

FundingRequest

答案 1 :(得分:0)

使ExtraCharge成为可编码

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