Iteration
Ranges can be used to iterate a fixed number of times, for example, for i in 1...12
. To print out these numbers, a loop such as the following can be used:
> for i in 1...12 { . println("i is \(i)") . }
If the number is not required, then the underscore (_
) can be used as a hole to act as a throwaway value. An underscore can be assigned to, but not read:
> for _ in 1...12 { . println("Looping...") . }
However, it is more common to iterate over a collection's contents using a for...in
pattern. This steps through each of the items in the collection, and the body of the for
loop is executed over each one:
> var shopping = [ "Milk", "Eggs", "Coffee", "Tea", ] > var costs = [ "Milk":1, "Eggs":2, "Coffee":3, "Tea":4, ] > var cost = 0 > for item in shopping { . if let itemCost = costs[item] { . cost += itemCost . } . } > cost cost: Int = 10
To iterate over a dictionary, it is possible to extract the keys or the values and process them as an array:
> Array(costs.keys) $R2: [String] = 4 values { [0] = "Coffee" [1] = "Milk" [2] = "Eggs" [3] = "Tea" } > Array(costs.values) $R3: [Int] = 4 values { [0] = 3 [1] = 1 [2] = 2 [3] = 4 }
Note
Note that the order of keys in a dictionary are not guaranteed; if the dictionary changes size, the order may change.
Converting a dictionary's values to an array is not performant, as this will result in a copy of the data being made. Instead, the underlying values are of a type MapCollectionView
, which provides an iterable internal view of the data structure:
> costs.keys $R4: LazyBidirectionalCollectionMapCollectionView<[String : Int], String>> = { _base = { _base = { [0] = { key = "Coffee" value = 3 } [1] = { key = "Milk" value = 1 } [2] = { key = "Eggs" value = 2 } [3] = { key = "Tea" value = 4 } } _transform = } }
To print out all the keys in a dictionary, the keys
property can be used with a for...in
loop:
> for item in costs.keys { . println(item) . } Coffee Milk Eggs Tea
Iterating over keys and values in a dictionary
Traversing a dictionary to obtain all of the keys and then subsequently looking up values will result in searching the data structure twice. Instead, both the key and the value can be iterated at the same time using a tuple. A tuple is like a fixed-sized array, but one that allows assigning pairs (or triplets and so on) of values at a time:
> var (a,b) = (1,2) a: Int = 1 b: Int = 2
Tuples can be used to iterate pairwise over both the keys and values of a dictionary:
> for (item,cost) in costs { . println("The \(item) costs \(cost)") . } The Coffee costs 3 The Milk costs 1 The Eggs costs 2 The Tea costs 4
Both Array
and Dictionary
conform to the SequenceType protocol, which allows them to be iterated with a for...in
loop. Collections (as well as other objects such as Range
) that implement SequenceType
have a generate
method, which returns a GeneratorType
that allows the data to be iterated over. It is possible for custom Swift objects to implement SequenceType
to allow them to be used in a for...in
loop.
Iteration with for loops
Although the most common use of the for
operator in Swift is in a for...in
loop, it is also possible to use a more traditional form of for
loop. This has an initialization, a condition that is tested at the start of each loop, and a step operation that is evaluated at the end of each loop. Although the parentheses around the for
loop are optional, the braces for the block of code are mandatory.
Calculating the sum of integers between 1 and 10 without using the range operator can be done as follows:
> var sum = 0 . for var i=0; i<=10; ++i { . sum += i . } sum: Int = 55
If multiple variables need to be updated in the for
loop, Swift has an expression list that is a set of comma-separated expressions. To step through two sets of variables in a for loop, the following can be used:
> for var i = 0,j = 10; i<=10 && j >= 0; ++i,--j { . println("\(i), \(j)") . } 0, 10 1, 9 … 9, 1 10, 0
Tip
Apple recommends the use of ++i
instead of i++
(and conversely, --i
instead of i--
) because they will return the result of i
after the operation, which may be the expected value.
Break and continue
The break
statement leaves the innermost loop early, and control jumps to the end of the loop. The continue
statement takes execution to the top of the innermost loop and the next item.
To break
or continue
from nested loops, a label can be used. Labels in Swift can only be applied to a loop statement such as while
or for
. A label is introduced by an identifier and a colon just before the loop statement:
> var deck = [1...13, 1...13, 1...13, 1...13] > suits: for suit in deck { . for card in suit { . if card == 3 { . continue // go to next card in same suit . } . if card == 5 { . continue suits // go to next suit . } . if card == 7 { . break // leave card loop . } . if card == 13 { . break suits // leave suit loop . } . } . }