I want to do extra logic after last item was processed, but terminal show that i
has always the same value as c
. Any idea how to pass the loop variable in?
let c = a.count
for var i=0; i<c; i++ {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {
// ..
dispatch_async(dispatch_get_main_queue(), {
println("i \(i) c \(c)")
if i == c-1 {
// extra stuff would come here
}
})
})
}
By the time your closure is executed, the for loop has already finished and i
= c
. You need an auxiliary variable inside the for loop:
let c = a.count
for var i=0; i<c; i++ {
let k = i
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {
// ..
dispatch_async(dispatch_get_main_queue(), {
println("k \(k) c \(c)")
if k == c-1 {
// extra stuff would come here
}
})
})
}
You can capture the value of i
explicitly with a capture list [i]
in the closure, then you don't need to copy it to a separate variable.
Example:
let c = 5
for var i=0; i<c; i++ {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {
[i] in // <===== Capture list
dispatch_async(dispatch_get_main_queue(), {
println("i \(i) c \(c)")
})
})
}
Output:
i 0 c 5 i 1 c 5 i 2 c 5 i 3 c 5 i 4 c 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With