【发布时间】:2019-08-17 02:04:45
【问题描述】:
我查看了类似问题的答案,我无法很好地理解codingKey,此外它并不完全适用于我的情况,因为密钥并非完全“未知”,它是前一个密钥的值. 我的 API:
{
"api": {
"results": 1,
"fixtures": [
{
"homeTeam": {
"team_name": "Tottenham"
},
"awayTeam": {
"team_name": "Everton"
},
"lineups": {
"Tottenham": {
"formation": "4-2-3-1"
},
"Everton": {
"formation": "4-2-3-1"
}
}
}
]
}
}
我的代码:
class matchApiObject: Decodable
{
let fixtures: [fixture]
init (fixtures: [fixture])
{
self.fixtures = fixtures
}
}
class fixture: Decodable
{
let homeTeam: matchHomeTeamObject?
let lineups: lineUpsObject?
init (homeTeam: matchHomeTeamObject?, lineups: lineUpsObject?)
{
self.homeTeam = homeTeam
self.lineups = lineups
}
}
class matchHomeTeamObject: Decodable
{
let team_name: String?
init (team_name: String?)
{
self.team_name = team_name
}
}
class lineUpsObject: Decodable
{
struct homeLineUp: Decodable
{
let formation: String?
init(formation: String?)
{
self.formation = formation
}
}
struct awayLineUp: Decodable
{
let formation: String?
init (formation: String?)
{
self.formation = formation
}
}
}
很明显,阵容对象的键不会是“homeLineUp”,而是按照 api 示例,即 homeTeam.team_name 的值。
所以我想象的解决方案是:
class lineUpsObject: Decodable
{
struct homeTeam.team_name: Decodable
{
let formation: String?
init(formation: String?)
{
self.formation = formation
}
}
struct awayTeam.team_name: Decodable
{
let formation: String?
init (formation: String?)
{
self.formation = formation
}
}
}
这是不可能的,我知道我必须为此使用编码键,但我不明白如何将键的名称声明为前一个键值的值 我不明白什么是 stringValue: String或 intValue: Int do in the codingkey answer do 或它们如何应用在这里,谢谢。
【问题讨论】: