我有一个设置为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]()
但是,这会产生错误:

通用结构“ObservedObject”要求“[MatchData]”符合
'ObservableObject'

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

最佳答案

您可以使obt_code的集合成为显式的可观察类,例如

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

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

10-09 18:30