调用初始化程序 SwiftUI 时没有完全匹配
No exact matches in call to initializer SwiftUI
我有以下代码。我在我的 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 文件中添加了新项目,但出于某种原因我无法显示 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