NEW: Master Swift design patterns with my latest book! >>

How to loop through items in an array

Written by Paul Hudson    @twostraws

Swift offers a selection of ways to loop through an array, but the easiest and fastest is known as fast enumeration and looks like this:

let array = ["Apples", "Peaches", "Plums"]

for item in array {
    print("Found \(item)")
}

That will print "Found Apples", "Found Peaches" then "Found Plums" to the Xcode console. Each time the loop goes around, one item is read from the array and placed into the constant item – and note that is a constant, so don't try to change it.

Available from iOS 7.0

Did this solution work for you? Please pass it on!

Other people are reading…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Get six books for only $150

The Swift Power Pack includes my first six books for one low price, helping you jumpstart a new career in iOS development – check it out!

Click here to visit the Hacking with Swift store >>