I have a PickerView inside a form that I want to default to a certain value. I pass an object called MatchEvent into the sheet and inside the MatchEvent there is a player attribute. Players are collected inside another class called Team. I have tried to loop through the team attribute players and return and index but I'm thinking there must be a more streamlined way. I want to have a picker view that is already set to the value passed in. Below is my picker view code:
@State var playerSelect = 0
Picker(selection: $playerSelect, label: Text("Select Player")) {
ForEach(0..<team.players.count) {
Text("\(team.players[$0].shirtNumber) - \(team.players[$0].playerName)")
}
.navigationBarTitle("Select Player")
}
Text("Selected - \(team.players[playerSelect].shirtNumber)")
Here is my team class code:
@Published var players: [Player] = [Player(playerName: "", shirtNumber: 1, active: true),
Player(playerName: "", shirtNumber: 2, active: true),
Player(playerName: "", shirtNumber: 3, active: true),
Player(playerName: "", shirtNumber: 4, active: true), //Carries on for 23 players
Any help would be appreciated, cheers!