Swift version: 5.6
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.
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until October 1st.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Available from iOS 7.0
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.