gpt4 book ai didi

json - 调用初始化程序 SwiftUI 时没有完全匹配

转载 作者:行者123 更新时间:2023-12-02 16:23:10 26 4
gpt4 key购买 nike

我有以下代码。我在我的 json 文件中添加了新项目,但由于某种原因我无法显示 team.Pos,即使我添加了所有模型等。原始数据有效但新添加的数据无效。知道为什么吗?此外,我还尝试显示数组数据。但同样它也有同样的错误

import SwiftUI
struct TeamRow: View {
var team: Team
var body: some View {
HStack {
Image(team.iconName)
Text(team.code)
Text(team.Pos) // the error happens here
Text(clubRanking[10]) // the same error happens
Spacer()

}
}
}

struct TeamRow_Previews: PreviewProvider {
static var previews: some View {
TeamRow(team: teamData[0])
.previewLayout(.fixed(width: 350, height: 70))
}
}

这是数据模型

import SwiftUI
import CoreLocation

struct Team: Hashable, Codable, Identifiable {
var id: Int
var name: String
var nickname: String
var iconName: String
var imageName: String
var stadiumName: String
var capacity: String
var city: String
var website: String
var code: String
var Pos: Int
var PL: Int
var W: Int
var D: Int
var L: Int
var GD: Int
var Pts: Int

}

这是json文件

[
{
"id":1,
"name": "Tottenham Hotspur",
"nickname": "Spurs",
"iconName": "Spurs",
"imageName": "Tottenham-Stadium",
"stadiumName": "Tottenham Hotspur Stadium",
"capacity": "62303",
"city": "London",
"website": "https://www.tottenhamhotspur.com",
"code":"TOT",
"Pos":1,
"PL":0,
"W":0,
"D":0,
"L":0,
"GD":0,
"Pts":0
},
{
"id": 2,
"name": "Liverpool F.C.",
"nickname": "Liverpool",
"iconName": "Liverpool",
"imageName": "Liverpool-Stadium",
"stadiumName": "Anfield Stadium",
"capacity": "53394",
"city": "Liverpool",
"website": "https://www.liverpoolfc.com",
"code":"LIV",
"Pos":1,
"PL":0,
"W":0,
"D":0,
"L":0,
"GD":0,
"Pts":0
},
]

最佳答案

尝试如下:

Text("\(team.Pos)") // this should work
Text("\(clubRanking[10])") // not clear what is clubRanking, but probably the same

关于json - 调用初始化程序 SwiftUI 时没有完全匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65110058/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com