我们如何为 ForEach SwiftUI 制作自定义 Step?

use*_*ser 2 swiftui

我有一个与索引一起使用的 ForEach 循环,我如何使这个 ForEach 步骤获得自定义步骤,因为在每个循环中的 ForEach 中,它将向索引添加一个,我们如何使步骤为 5,这意味着在每个循环中添加 5 而不是 1。我可以使用 if 和 % 来实现,但我不希望 ForEach 尝试每个 Items 中的每个 Item。我们如何才能使其高效编码?

代码:

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

    }
}

 
Run Code Online (Sandbox Code Playgroud)

Asp*_*eri 5

这是可能的方法

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

}
Run Code Online (Sandbox Code Playgroud)