gpt4 book ai didi

ios - 将 ScrollView 与 Array 一起使用时出现问题

转载 作者:行者123 更新时间:2023-12-01 16:19:48 24 4
gpt4 key购买 nike

每当通过调用 Web 服务更新数组时,我都会尝试使用数组来显示一些游戏信息。数组填充正常,但在运行时我得到:

Generic struct 'ForEachWithIndex' requires that 'Binding<[MatchData]>'conform to 'RandomAccessCollection'


我的 ScrollView :
ScrollView{
ForEach(GameCenterHelper.helper.$MatchList, id: \.self) {row in
ext("\(row.id)")
}
}
我的数组声明:
@State var MatchList: [MatchData] = [MatchData]()
和匹配数据:
class MatchData : Identifiable {

var id: String

var LocalPlayer: Player
var RemotePlayer: Player

init(_ match: GKTurnBasedMatch) {

let local = match.participants[0].player!
let remote = match.participants[1].player ?? GKPlayer()
self.id = match.matchID

LocalPlayer = Player(Alias: local.alias
, DisplayName: local.displayName
, TeamPlayerId: local.teamPlayerID
, PlayerId: local.gamePlayerID
)

RemotePlayer = Player(Alias: remote.alias
, DisplayName: remote.displayName
, TeamPlayerId: remote.teamPlayerID
, PlayerId: remote.gamePlayerID

)
}
}
我不得不承认,这是我第一次使用状态来尝试刷新 ScrollView,但我发现它比我预期的要困难得多,而且我不确定我的代码中出了什么问题。

最佳答案

您无需绑定(bind)即可迭代 MatchListForEach , 直接访问

ScrollView{
ForEach(GameCenterHelper.helper.MatchList, id: \.self) {row in // << no $
ext("\(row.id)")
}
}

关于ios - 将 ScrollView 与 Array 一起使用时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63324496/

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