Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 快速理解何时是整数数组上for循环的最后一次迭代_Arrays_Swift_For Loop_Integer_Iteration - Fatal编程技术网

Arrays 快速理解何时是整数数组上for循环的最后一次迭代

Arrays 快速理解何时是整数数组上for循环的最后一次迭代,arrays,swift,for-loop,integer,iteration,Arrays,Swift,For Loop,Integer,Iteration,我的Swift代码中有一个for循环,它迭代整数数组 var indexes:[Int] = [] .... .... for oneInt in indexes { if (oneInt==indexes.last) {doSomethingElse(oneInt)} else {doSomething(oneInt)} } 我需要的是理解什么时候是最后一个元素

我的Swift代码中有一个for循环,它迭代整数数组

var indexes:[Int] = []
....
....
for oneInt in indexes
        {
            if (oneInt==indexes.last) {doSomethingElse(oneInt)}
                                      else {doSomething(oneInt)}
        }
我需要的是理解什么时候是最后一个元素

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

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

还是应该重写for循环

斯威夫特似乎没有传统的for循环

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

使用枚举:

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

一个问题太多(做的研究太少)。阅读关于循环的文章。在阅读Swift编程语言书的同时,也可以查看关于在index.dropLast()中为oneInt删除集合的最后一个元素
,并将doSomething添加到
oneInt
中,然后在let last=index.last的情况下获取集合的最后一个元素
,这样会容易得多{
和doSomethingElse与
last
。如果索引等于最后一个,则无需在每次迭代中进行检查。