Reputation: 29314
Say I have the following code:
var array = [1, 5, 6, 2, 7, 4]
for item in array {
println(index)
}
Is there a way to access what iteration the loop is on within the loop's body? (i.e.: 0, 1, 2, 3...)
Upvotes: 1
Views: 866
Reputation: 11868
var array = [1, 5, 6, 2, 7, 4]
for (index,item) in enumerate(array) {
println("\(index): \(item)")
}
Upvotes: 11
Reputation: 4604
You can create a count variable. Other than that, you need to use a regular for loop.
Upvotes: 0