3 ms·
You have access to the index in a for-in: for (index, element) in list.enumerate() { print("Item \(index): \(element)") }
by lukeholder 10y ago
You have access to the index in a for-in:
for (index, element) in list.enumerate() {
print("Item \(index): \(element)")
}
- stevetrewick 10y agoNitpick: for...in is not providing access to the index, it's the enumerate() method returning a collection of tuples, the first element of which happens to be an index.
- jathu 10y agofor-in is elegant, and you can use enumerate() to access index. However, if we had to do reverse for-loops in Swift 3, we would have to use for-in with either from.stride(to:by:) or reverse(), and to me that is not as elegant as c style for loops.
- masklinn 10y agoInteresting, it feels significantly more elegant to me as you're simply telling the language what you want (reverse iteration) and it handles the exact details, and works for any reversible iterable.