도래울

swift for문 index알기 본문

개발/iOS

swift for문 index알기

도래울 2016. 5. 26. 14:51

Yes. As of Swift 2.0, if you need the index for each element along with its value, you can use the enumerate() method to iterate over the array. It returns a tuple composed of the index and the value for each item in the array. For example:

for (index, element) in list.enumerate() {
    print("Item \(index): \(element)")
}

Prior to Swift 2.0, enumerate was a global function.

for (index, element) in enumerate(list) {
    println("Item \(index): \(element)")
}


Comments