I have a struct of the following type populated from a .json. The struct is basically a football fixtures list in which each fixture has a date, a competition and a home and away team playing in that competition on that date.
struct Fixtures: Codable, Identifiable {
let id: Int
let dateid: Int
let compid: Int
let homeTeam: Int
let awayTeam: Int
}
I'm trying to loop over this data using ForEach
and whilst I can do this once using a member such as .filter
I can't seem to do it on more than one level using nesting. For example, if I want to show ALL matches in ALL competitions on a certain day, this following code works:
let fixtures: [Fixtures] = bundle.main.decode(file: "fixtureTable.json")
@State private var dateFilter: Int = 1
List {
ForEach(fixtures.filter { $0.dateid == dateFilter}) { dateFiltered in
Text("\(dateFiltered.homeTeam) vs \(dateFiltered.awayTeam)")
}
}
but if I want to filter it further, i.e. to show ALL matches in only a certain competition on a certain day, when I use the following code it throws the error Value of type 'Fixtures' has no member 'filter' This error comes frm the second ForEach line.
let fixtures: [Fixtures] = bundle.main.decode(file: "fixtureTable.json")
@State private var dateFilter: Int = 1
@State private var competitionFilter: Int = 1
List {
ForEach(fixtures.filter { $0.dateid == dateFilter}) { dateFiltered in
ForEach(dateFiltered.filter { $0.compid == competitionFilter}) { compFiltered in
Text("\(compFiltered.homeTeam) vs \(compFiltered.awayTeam)")
}
}
}
For reference the above example has been simplified somewhat and the ultimate aim will be to show a list similar to the below using Section for the different competitions. As such compid will ultimately not be filtered but rather will require a function that only shows unique values but the fact that I cant seem to use any member for the second nested ForEach seems to be the over-riding issue.
Any help on how to fix this or whether I am going about this in the wrong way would be appreciated. Equally, a function for only unique values in a list would also be gratefully appreciated!
12th September 2021
Premier League
Team A vs Team B
Team C vs Team D
Championship
Team E vs Team F
Team G vs Team H
...
League 1
...etc