I want to iterate an array using ForEach
and, depending on the use-case, its sub-version with fewer elements.
ForEach(isExpanded ? $items : $items[..<4])
The problem is if I am using a subscript, I am getting an error Result values in '? :' expression have mismatching types 'Binding<[RowItem]>' and 'Binding<[RowItem]>.SubSequence' (aka 'Slice<Binding<Array<RowItem>>>')
. I can not use it like this, because it is not an array but a Slice
. Just casting to Array
does not work either.
2
Answers
You don’t have to use $ sign in front of your arrays.
This is a sample code which responds to the poster’s second question.
Use binding to store/pass/receive data from another struct:
However, If your data only works within one struct, and you don’t need to pass the data back and forth within another struct, use @State: