0

I have an empty array var choosenData = [Choosen]() By tapping a button, I append myView() component inside this array (component stored in another array)

self.choosenstore.choosenarray.append(
    Choosen(value: self.collectionarray[index].value)
)

And I would like to display this array like this:

HStack {
    ForEach(choosenstore.choosenarray) { choosen in
        myView(myValue: choosen.value)

    }
}

But on multiple lines. I tried multiple solution I saw on StackOverflow and other but it didn't work. So maybe is it possible to like do a "ForEach(choosenstore.choosenarray between 0 and 4 )" then a "ForEach(choosenstore.choosenarray between 4 and 8 )" ect... ?

Thanks !

1 Answer 1

1

It's an array. So you can deal with it like a normal array in Swift. Something like:

    HStack {
        ForEach(
            choosenstore.choosenarray
                .enumerated()
                .filter { (4...8).contains($0.offset) } // Here is the filter for being between 4 and 8
                .map { $1 }) { choosen in
            myView(myValue: choosen.value)
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.