How do I write a for-loop in Swift 3 for an array that I modify during the for loop?

柔情痞子 提交于 2019-12-31 10:17:25

问题


So, I have a for-loop that looks similar to this:

for var i = 0; i < results.count ; i += 1 {
   if (results[i] < 5) {
      results.removeAtIndex(i)
      i -= 1
   }
}

This used to work. But when I changed it to the preferred Swift 3.0 syntax:

for var i in 0..<results.count {
   if (results[i] < 5) {
      results.removeAtIndex(i)
      i -= 1
   }
}

I get an array IOOBE exception because it doesn't re-check the count and continues on until the original results.count.

How do I fix this? It works now, but I don't want to get into trouble in the future.


回答1:


While the solution making use of filter is a fine solution and it's more Swift-ly, there is another way, if making use of for-in is, nonetheless, still desired:

var results = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for var i in (0..<results.count).reverse()
{
    if (results[i] < 5)
    {
        results.removeAtIndex(i)
        //i -= 1
    }
}

print(results)

Result:

[5, 6, 7, 8, 9, 10]

We could omit this line i -= 1 altogether, in addition.

The problem with removeAtIndex within the loop is that it will not cause the array to re-index itself in-place and thus causing an array out of bounds exception due to count not being updated.

By traversing backwards, the out of bounds exception can thus be avoided.




回答2:


Could you use a filter instead?

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let greaterThan5 = numbers.filter{$0 >= 5}
print(greaterThan5)



回答3:


If you want to continue using a for-loop, you can enumerate over both index and element using enumerate:

for (index, element) in results.enumerate() {
   if (element < 5) {
     results.removeAtIndex(index)
   }
}

Although depending on what you're doing in your loop, the filter method might be a better idea.




回答4:


If your loop goes forward...

for var i in (0..<results.count) where results.indices.contains(i) { 

//if the index doesn't exist, the loop will be stopped.

if (results[i] < 5) {
        results.removeAtIndex(i) 
    }

}


来源:https://stackoverflow.com/questions/37596063/how-do-i-write-a-for-loop-in-swift-3-for-an-array-that-i-modify-during-the-for-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!