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

Iterating over an array

We can iterate over all elements of an array, in order, with a for-in loop. The for-in loop will execute one or more statements for each element of the array. We will discuss the for-in loop in greater detail in Chapter 4, Control Flow and Functions. The following example shows how we would iterate over the elements of an array:

var arrayOne = ["one", "two", "three"]  
for item in arrayOne { 
  print(item) 
} 

In the preceding example, the for-in loop iterates over the array and executes the print(item) line for each element in the array. If we run this code, it will display the following results in the console:

one  
two  
three 

There are times when we would like to iterate over an array, as we did in the preceding example, ...

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