UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How do I nest ForEach loops? Current code erroring with Value of type '_' has no member '_'

Forums > SwiftUI

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

2      

Because dateFiltered is a single element from the array fixtures you loop over of and not an array itself. That's why you get the error message has no member.

2      

OK - thanks for the feedback. I think I understand that. If that is the case though what is my best approach to achieve what I want. Is it still possible to create a series of filters on this one large data model or should I create a series of smaller models outside of the view specific to this individual task to populate the list? I dont expect anyone to write the code for me but either a broad overview and/or pseudo code or a link to an example would be great. Thanks again.

2      

What you could do is create two additional arrays from your huge dataset. One for the date, one for the league.

Then you loop over the date array, in this a nested array and loop over the league and a third nested array filtering for the date and the league for your matches.

I don't know if this works with your data model but this would be an approach. Depends on how much data you want to show.

2      

You could have the array be filtered before the ForEach

I have done a filtered list package that you can look at or use.

AFfilterList

Hope that give you some ideas

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.