How to filter a segmented picker in SwiftUI?

I'm trying to change change the segmented picker options depending on other user selection. But the segmented picker is not even showing.

Code Block Swift
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(entity: GameDeck.entity(), sortDescriptors: []) var decks: FetchedResults<GameDeck>
@State private var yourClass: String?
@State private var selectedDeck = 0

Code Block Swift
Picker(selection: $selectedDeck, label: Text("")) {
ForEach(0..<decks.filter{$0.heroClass == yourClass}.count) {
Text(self.decks[$0].wrappedName)
}
}
.pickerStyle(SegmentedPickerStyle())

What I'm trying to do is filtering the decks in which "heroClass" equals "yourClass" to show all those decks in the picker. "yourClass" selection is working properly. I'm using CoreData for the decks.
Answered by ChrisFrost in 612673022
I think the line

ForEach(0..
ist the problem. First of all, use ' , id: \.self ' after count, before the closing bracket - but this will not solve the problem.

The second thing is, that Your "filtering" is only producing the smaller "count" number, but You still address the whole "decks" array, and it will show only several first results of the array (the number will be equal to how many matches were found), but not the ones, You want to show, as I suppose.

Also please try to unpack ' $0.heroClass ' like this: ' $0.heroClass ?? "" '

Hope I could help.
Accepted Answer
I think the line

ForEach(0..
ist the problem. First of all, use ' , id: \.self ' after count, before the closing bracket - but this will not solve the problem.

The second thing is, that Your "filtering" is only producing the smaller "count" number, but You still address the whole "decks" array, and it will show only several first results of the array (the number will be equal to how many matches were found), but not the ones, You want to show, as I suppose.

Also please try to unpack ' $0.heroClass ' like this: ' $0.heroClass ?? "" '

Hope I could help.
How to filter a segmented picker in SwiftUI?
 
 
Q