O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

forEach

We can use the forEach algorithm to iterate over a sequence. The following example shows how we would do this:

var arrayOne = [10, 20, 30, 40]  
arrayOne.forEach{ print($0) } 

This example will print the following results to the console:

10 
20 
30 
40 

While using the forEach algorithm is very easy, it does have some limitations. The recommended way to iterate over an array is to use the for-in loop, which we will see in the next section.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required