.forEach()

The .forEach() method loops over the array, calling the closure function on each element in the array. As each element is iterated over, some action can be performed on it.

This method will operate on all elements of the array and cannot be escaped using a break or continue call. Returning a value in the body of the closure will only return that value out of the body and not the declaring method body. The .forEach() method is commonly used when all elements are to be manipulated by the loop or to display the purpose of the loop to others reading the code.

Syntax

arrayName.forEach(body)

Example

var topBabyNames = ["Liam", "Jackson"]
topBabyNames.forEach { name in
print("\(name) is the coolest name.")
}
// Output:
// Liam is the coolest name.
// Jackson is the coolest.

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Swift on Codecademy