1

I want to traverse an array with a stride. For example, I have an array [0,1,2,3,4,5,6,7,8,9].And I want to traverse this array with a stride of 3. The oc code likes below:

for (int i = 0; i < array.count; i += 3) {

}

How can I do that with swift.

dowZhang
  • 470
  • 1
  • 3
  • 18

1 Answers1

1

You can use a for loop with stride to traverse the array

let testarray = [0,1,2,3,4,5,6,7,8,9]

for i in stride(from: 0, to: testarray.count, by: 3) {
    print(testarray[i])
}
byaruhaf
  • 4,128
  • 2
  • 32
  • 50