gpt4 book ai didi

ios - ObservableObject 的问题

转载 作者:行者123 更新时间:2023-12-01 21:27:04 27 4
gpt4 key购买 nike

我有一个设置为 ObservableObject 的类:

class MatchData : Identifiable,  ObservableObject {

@Published var id: Int

@Published var LocalPlayer: Player
@Published var RemotePlayer: Player

init(_ match: GKTurnBasedMatch, id: Int) {

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

)
self.id = id
}
}
然后在另一个类(class)我有:
@ObservedObject var MatchList: [MatchData] = [MatchData]()
但是,这会产生错误:

Generic struct 'ObservedObject' requires that '[MatchData]' conform to'ObservableObject'


我相信我的类符合 ObservableObject,但是我似乎无法摆脱这个错误。

最佳答案

您可以为 MatchData 的集合创建显式可观察类, 像

class MatchList: ObservableObject {
@Published var data: [MatchData] = [MatchData]()
}
然后你就可以在 View 中使用
@ObservedObject var matchList: MatchList

关于ios - ObservableObject 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63326784/

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