Home > OS >  Foreach indice has no member "element"
Foreach indice has no member "element"

Time:01-31

ForEach(Array(games[index1].items.enumerated()).filter{ searchitem.isEmpty ? true : 

$0.element.name.localizedCaseInsensitiveContains(searchtext)}, id: \.element){ (index,item) in

I have foreach loop and using enumerated,but for some reason,i need to change enumerated to indices to only get the index

ForEach(games[index1].items.indices.filter{ searchtext.isEmpty ? true : 

$0.element.name.localizedCaseInsensitiveContains(searchtext)}, id: \.self) { index in

the compyler complain." Value of type 'Range<Array.Index>.Element' (aka 'Int') has no member 'element' "

I try $0.self.name, but it's no work. anyideas?

CodePudding user response:

You are iterating your items indices therefore $0 gives you the index of that collection. What you need is to subscript items collection with that index to get your collection element:

games[index1].items[$0].name.localizedCaseInsensitiveContains(searchtext)
  •  Tags:  
  • Related