Swift delay in loop
try multiplying delay with index
func start(){
for index in 1...3 {
delay(3.0 * index){
println(index)
}
}
}
If you are (bizarrely) seeing 3 as the value of index that gets printed out on each of the iterations of the loop, you can do the following to ensure the correct (current) value of the iterator variable gets captured for the closure:
func start() {
for index in 1...3 {
let i = index
delay(3.0 * i) {
print(i)
}
}
}
I am not seeing that behaviour though with Xcode 7.3 & Swift 2.2 – the values printed are 1, 2, 3 with your version of the delay function. Are you perhaps using a very old version of Swift? This blog post actually covers the Swift for loop behaviour with value capture.
As noted in the other answer, multiplying the index with 3.0 accomplishes the 3,6,9 second delays.