更新时间:2022-04-24 00:35:40
尝试在GameInformation初始化中设置如下赔率:
Try setting odds like this in the GameInformation init:
odds = try container.decode(Array<Odds>.self, forKey: .odds)
下一步,您应该删除OddsKey枚举并更新Odds init:
Next you should remove OddsKey enum and update Odds init:
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
moneyLineHome = try container.decodeIfPresent(String.self, forKey: .moneyLineHome) ?? "N/A"
moneyLineAway = try container.decodeIfPresent(String.self, forKey: .moneyLineAway) ?? "N/A"
pointSpreadHome = try container.decodeIfPresent(String.self, forKey: .pointSpreadHome) ?? "N/A"
pointSpreadAway = try container.decodeIfPresent(String.self, forKey: .pointSpreadAway) ?? "N/A"
pointSpreadHomeLine = try container.decodeIfPresent(String.self, forKey: .pointSpreadHomeLine) ?? "N/A"
pointSpreadAwayLine = try container.decodeIfPresent(String.self, forKey: .pointSpreadAwayLine) ?? "N/A"
totalNumber = try container.decodeIfPresent(String.self, forKey: .totalNumber) ?? "N/A"
overLine = try container.decodeIfPresent(String.self, forKey: .overLine) ?? "N/A"
underLine = try container.decodeIfPresent(String.self, forKey: .underLine) ?? "N/A"
drawLine = try container.decodeIfPresent(String.self, forKey: .drawLine) ?? "N/A"
lastUpdated = try container.decodeIfPresent(String.self, forKey: .lastUpdated) ?? "N/A"
oddType = try container.decodeIfPresent(String.self, forKey: .oddType) ?? "N/A"
}
我没有测试过,但是应该可以。关键是,我们应该将赔率解析为init内的单个对象,并将其解析为GameInformation init内的一个集合。我希望这是有道理的。
I haven't tested it, but it should work. The thing is, we should parse Odds as a single object inside it's init, and as a collection inside of GameInformation init. I hope it makes sense.
我认为***在解析后进行过滤,您可以在GameInformation init中初始化这样的赔率(但是如果没有,这似乎是不正确的
I think it's better to filter after parsing, you can initialize odds like this in the GameInformation init (but it just doesn't seem right without utilizing optionals):
odds = try container.decode(Array<Odds>.self, forKey: .odds).filter {
let mirror = Mirror(reflecting: $0)
return !mirror.children.contains(where: { _, value in
if let v = value as? String, v == "N/A" {
return true
} else {
return false
}
})
}