To iterate over an array in Swift, you can use a for-in
loop or array methods like forEach
.
In this example,
numbers
with some values.for-in
loop to iterate over each element of the array and print the elements.let numbers = [1, 2, 3, 4, 5]
for number in numbers {
print(number)
}
1 2 3 4 5
In this example,
fruits
with some values.forEach
method to iterate over each element of the array and print the elements.let fruits = ["apple", "banana", "cherry"]
fruits.forEach { fruit in
print(fruit)
}
apple banana cherry
In this tutorial, we learned How to Iterate Over an Array in Swift language with well detailed examples.