为什么会出现错误:类型“ MenuItem”不符合协议“可解码”?以前在工作。我更改的一件事是[String]的修饰符?到[修饰符]?那会产生错误吗?如果是这样,为什么? Stackoverflow希望我继续说些什么,因为相对于我在下面显示的代码量来说,它的描述太少了。仍然需要添加文本。
struct MenuItem: Codable {
let itemId: String
let name: String
var modifiers: [Modifier]?
var photoUrl: String?
var quantity: Int
var unitPrice: Int
var sizeAddOnPrice: Int
var toppingsAddOnPrice: Int
var totalPrice: Int
var totalModifiersPrice: Int
let description: String
var size: String
var toppings: [String]?
let category: String
init(itemId: String, name: String, modifiers: [Modifier]?, photoUrl: String?, quantity: Int, unitPrice: Int, sizeAddOnPrice: Int, toppingsAddOnPrice: Int, totalPrice: Int, totalModifiersPrice: Int, description: String, size: String, toppings: [String]?, category: String) {
self.itemId = itemId
self.name = name
self.modifiers = modifiers
self.photoUrl = photoUrl
self.quantity = quantity
self.unitPrice = unitPrice
self.sizeAddOnPrice = sizeAddOnPrice
self.toppingsAddOnPrice = toppingsAddOnPrice
self.totalPrice = totalPrice
self.totalModifiersPrice = totalModifiersPrice
self.description = description
self.size = size
self.toppings = toppings
self.category = category
}
init?(itemId: String, payload: JSON) {
guard
let name = payload[ParamKey.name].string,
let photoUrl = payload[ParamKey.photoUrl].string,
let description = payload[ParamKey.description].string,
let category = payload[ParamKey.categoryName].string,
let unitPrice = payload[ParamKey.basePrice].int,
let size = payload[ParamKey.size].string
else { return nil }
self.itemId = itemId
self.name = name
self.photoUrl = photoUrl
self.description = description
self.category = category
self.unitPrice = unitPrice
self.size = size
self.sizeAddOnPrice = 0
self.toppings = nil
self.toppingsAddOnPrice = 0
self.totalPrice = (unitPrice + sizeAddOnPrice + toppingsAddOnPrice) * quantity
self.totalModifiersPrice = (sizeAddOnPrice + toppingsAddOnPrice) * 2
self.quantity = 1
self.modifiers = payload[ParamKey.modifiers].arrayObject as? [Modifier]
}
}
答案 0 :(得分:2)
您还需要使内部结构符合Decodable
/ Codable
并删除结构init
,它将自动生成
struct Modifier: Codable {
// add properties
}
struct MenuItem: Codable {
let itemId: String
let name: String
var modifiers: [Modifier]?
var photoUrl: String?
var quantity: Int
var unitPrice: Int
var sizeAddOnPrice: Int
var toppingsAddOnPrice: Int
var totalPrice: Int
var totalModifiersPrice: Int
let description: String
var size: String
var toppings: [String]?
let category: String
init?(itemId: String, payload: JSON) {
guard
let name = payload[ParamKey.name].string,
let photoUrl = payload[ParamKey.photoUrl].string,
let description = payload[ParamKey.description].string,
let category = payload[ParamKey.categoryName].string,
let unitPrice = payload[ParamKey.basePrice].int,
let size = payload[ParamKey.size].string
else { return nil }
self.itemId = itemId
self.name = name
self.photoUrl = photoUrl
self.description = description
self.category = category
self.unitPrice = unitPrice
self.size = size
self.sizeAddOnPrice = 0
self.toppings = nil
self.toppingsAddOnPrice = 0
self.totalPrice = (unitPrice + sizeAddOnPrice + toppingsAddOnPrice) * quantity
self.totalModifiersPrice = (sizeAddOnPrice + toppingsAddOnPrice) * 2
self.quantity = 1
self.modifiers = payload[ParamKey.modifiers].arrayObject as? [Modifier]
}
}
您也可以使用
try content.rawData() // for payload: JSON) {
将JSON
转换为数据,然后将其提供给Decodable
类