Swift - 了解何时是整数数组的 for 循环中的最后一次迭代

Swift - Understanding when it is the last iteration in for loop over an array of integers

我的 Swift 代码中有一个 for 循环遍历整数数组。

var indexes:[Int] = []
....
....
for oneInt in indexes
        {
            if (oneInt==indexes.last) {doSomethingElse(oneInt)}
                                      else {doSomething(oneInt)}
        }

我的需求是了解什么时候是最后一个元素。

使用提供的“最后”值,我认为我有一个整数,而不是一个对象。

那么我如何比较这些值才能知道我有最后一个?

或者我应该重写 for 循环吗?

Swift好像没有传统的for循环。

我必须使用计数器变量吗?

使用枚举:

for (index, oneInt) in indexes.enumerated() {
if (index==indexes.count-1) { doSomething(oneInt) } 
                          else { doSomethingElse(oneInt) }
}