Hello,
I have a JSON file containing the data below. I was wondering how can I just get the Team Names only once. I am able to get the records and display in my picker but it comes out like this. I know why it does it but I just cant figure out how to get just the unique values. Still very new to this.
_**_If I filter on Tee Ball I get
Team #1 Team #1 Team #2_**_
_**or if I filter on Coach Pitch I get
Team #10 Team #10 Team #2**_
I want it to only display the teams once so if I pass in Tee Ball it only shows
Team #1 Team #2
[
{
"TeamName": "Team #1",
"DivisionName": "Tee Ball",
"PlayerFirstName": "Joe",
"PlayerLastName": "Smith"
},
{
"TeamName": "Team #10",
"DivisionName": "Coach Pitch",
"PlayerFirstName": "Jane",
"PlayerLastName": "Jones"
},
{
"TeamName": "Team #10",
"DivisionName": "Coach Pitch",
"PlayerFirstName": "Peter",
"PlayerLastName": "Parker"
}, {
"TeamName": "Team #1",
"DivisionName": "Tee Ball",
"PlayerFirstName": "Reed",
"PlayerLastName": "Richards"
}, {
"TeamName": "Team #2",
"DivisionName": "Tee Ball",
"PlayerFirstName": "Brent",
"PlayerLastName": "Barry"
}, {
"TeamName": "Team #2",
"DivisionName": "Coach Pitch",
"PlayerFirstName": "Harry",
"PlayerLastName": "Hunt"
},
]
Code to pull JSON data
func getLocalData() {
// Get a url to the jsons file
let jsonsUrl = Bundle.main.url(forResource: "Book2", withExtension: "json")
do{
// Read the file into a data object
let jsonData = try Data(contentsOf: jsonsUrl!)
//Try to decode the json into an array of players
let jsonDecoder = JSONDecoder()
let players = try jsonDecoder.decode([Player].self, from: jsonData)
//Set unique IDs for each instance
for p in players {
p.id = UUID()
// Set a unique ID for each recipe in the player array
}
// Assign parsed players to players property
self.players = players
}
catch{
// TODO log error
print("Couldn't parse local data")
}
```
Picker in my view
`Picker("Please select a team", selection:$selectedTeam) {
if buttonColorFlag == false {
Text("Select a Team").tag(selectedTeam)
}
ForEach(player.players) {player in
if player.DivisionName == selectedDivision{
Text(player.TeamName!).tag(player.TeamName!)
}
}
}
.multilineTextAlignment(.leading)
.frame(width: 200)`
Thanks in advance