create foreach in swift

To create a for each loop in Swift, you can use the for-in loop.

Here's an example that shows how to iterate over an array of strings:

main.swift
let fruits = ["apple", "banana", "orange", "kiwi"]

for fruit in fruits {
    print(fruit)
}
93 chars
6 lines

This will output:

main.swift
apple
banana
orange
kiwi
25 chars
5 lines

In this example, we declare an array of strings called fruits, which contains four elements. We then use a for-in loop to iterate over each element in the array. For each iteration, the value of the current element is assigned to the constant fruit. We then print the value of fruit.

This works for iterating over any collection that conforms to the Sequence protocol in Swift.

gistlibby LogSnag