How we can make custom Step for ForEach SwiftUI?

Viewed 1049

I have a ForEach loop which works with index, how I can make this ForEach step get custom step, because in ForEach in every single loop, it will add one to index, how we cam make step be 5, it means in every single loop adds 5 instead 1. I can make it possible with if and %, but I do not want ForEach try every each Item inside Items. how we can make it efficient coding?

Code:

 struct ContentView: View
{
    var body: some View
    {
        
        
        List
        {
            ForEach(0 ..< 21) { index in // (Step == 5) not 1
                Text("Hello, world!")
                    .padding()
            }
  
        }
 

    }
}

 
1 Answers

Here is possible approach

List
{
    ForEach(Array(stride(from: 0, to: 21, by: 5)), id: \.self) { index in // (Step == 5) not 1
        Text("Hello, world!")
            .padding()
    }

}
Related