Iterating over an array

Remember the for-in loop you studied in the previous chapter? You can use it to iterate over every item in an array. Type and run the following code:

// Iterating over an array
for shoppingListItem in shoppingList {
print(shoppingListItem)
}

This prints out every item in the array to the Debug area.

You can also use one-sided range operators. These are range operators with the value only on one side, for example, 1.... Type in and run the following code:

// one-sided range operators
for shoppingListItem in shoppingList[1...] {
print(shoppingListItem)
}

This prints out items starting with index 1, and continuing till the end of the array, to the Debug area.

This concludes the section on arrays, so now let's look at the second collection type, dictionaries. Dictionaries allow you to store a list of key-value pairs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.67.40