Game Development Reference
In-Depth Information
tionary. Drop this code into the body of your main.swift file and run it. You will see
the following output:
The abbreviation for New Mexico is NM
The abbreviation for Utah is UT
The abbreviation for Colorado is CO
This is a handy method of iterating over dictionaries.
while Loops
Swift's while loop implementations are, for the most part, just like Objective-C's. There
are two while loop implementations: the standard while , with the condition at the be-
ginning of the loop, and the do-while loop, with the condition at the end. The only real
difference in these loops and the Objective-C equivalents is the optional parentheses.
while
The while loop first tests its condition, and if the result is true, then it executes the code
block until the condition is false. This code will check to see whether the index variable is
less than the number of elements in the colors array. If the result is true, then the code
in the braces is executed. This behavior will repeat until the index is no longer less than
the number of elements in the array.
var colors = ["red", "blue", "green", "black", "blue",
"orange"]
var index = 0
while index < colors.count {
println("The color is : \(colors[index])")
index++
}
do-while
The do-while loop has its conditional at the end of the loop. This means its code block
will be executed one time before the condition is checked. If the variable index equaled
10000 , the code would still be executed at least once. The program would crash because
Search WWH ::




Custom Search