Gavin Beard
Gavin Beard

Reputation: 139

Issue with ObservableObject

I have a class that is set to be an 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
    }
}

and then in a different class I have:

@ObservedObject var MatchList: [MatchData] = [MatchData]()

However, this produces the error:

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

I believed my class to conform to ObservableObject, however I do not seem to be able to get rid of this error.

Upvotes: 1

Views: 2035

Answers (1)

Asperi
Asperi

Reputation: 257711

You can make explicit observable class for collection of MatchData, like

class MatchList: ObservableObject {
   @Published var data: [MatchData] = [MatchData]()
}

and then you will be able to use in view

@ObservedObject var matchList: MatchList

Upvotes: 3

Related Questions