简体   繁体   中英

How to loop through arrays starting at different index while still looping through entire array in swift?

I want to start loop through array at index (say “5” or any other) instead of beginning of the array in swift

for (index, value) in array.enumerate() {
  // do something 
}

Use dropFirst to ignore the first items:

for (index, value) in array.enumerated().dropFirst(5) {
    // your code here
}

Use the where clause

for (index, value) in array.enumerate() where index > 4 { ...

By the way: Update your Swift version. Swift 2 is dead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM