【问题标题】:No exact matches in call to initializer SwiftUI调用初始化程序 SwiftUI 时没有完全匹配
【发布时间】:2021-03-14 12:41:44
【问题描述】:

我有以下代码。我在我的 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
    },
]

【问题讨论】:

    标签: json view swiftui swift5 xcode12


    【解决方案1】:

    尝试如下:

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

    【讨论】:

      猜你喜欢
      • 2022-11-19
      • 1970-01-01
      • 2022-01-15
      • 2022-08-02
      • 1970-01-01
      • 1970-01-01
      • 2020-08-22
      • 2020-10-29
      • 1970-01-01
      相关资源
      最近更新 更多