Home > Back-end >  How can I loop Swift 5 array indices offset to certain number using ForEach in view?
How can I loop Swift 5 array indices offset to certain number using ForEach in view?

Time:10-06

How can I loop an array using ForEach offset to a number

I have an array cities with a size of 10, how can I loop using this array 15 times?

ForEach(cities.indices, id: \.self) { index in
}

I am looking for a way to increment by 5 or any Int

I am initialising an array of cities size and adding the offset, thought there is a cleaner solution using indices but I cant find anything on the documentation

CodePudding user response:

You can do something like this:

ForEach(0..<cities.count   5, id:\.self) { index in
let city = cities[index]
}

Be careful. The loop will crash if it tries to access the index out of reach.

  • Related