In this Swift tutorial we will create variables of both Array and Dictionary types. We'll also play around with iterating through each using a for loop.
In the Swift code below, we first setup an Int variable equal to 2. Then we create an Array variable named levels which will store hypothetical level names. On the last line, we create a variable named currentLevelName which equals an item in the levels Array. Which item that is, is determined by whatever number (or variable for a number) is between the brackets.
var currentLevel:Int = 2 let levels = ["Swamp", "Volcano", "Abandoned High School", "Haunted Wal-Mart" ] var currentLevelName = levels[currentLevel] // would equal "Abandoned High School"
var currentLevelName = levels[currentLevel] // would equal "Abandoned High School"
var currentLevelName = levels //would equal the first item, "Swamp"
…but then we wouldn't have been able to make use of our currentLevel variable. Just remember the first item in an array is at an index value of 0, the second item is at an index of 1, and so on. Arrays have an order to them, and are considered an ordered list or ordered collection.
Side note: I might not have mentioned this in the video, but arrays can contain any value type. In this case, the value is inferred to be String.
Continuing with the previous code, we will now iterate through the levels array.
In the first line, you can think of the statement as being read like this, "for every single level we find in levels, do this chunk of code". Since levels has 4 items in it, the code within the brackets will run 4 times. Unless we call break, which will stop the for loop from iterating anymore. So if we are iterating through an array (or anything), and find a condition that warrants stopping the loop, thats an option.
In that example, we test to see if currentLevelName is equal to whatever level is in this iteration. Keep in mind that the value of level will change each iteration. So the first time around, level will equal "Swamp", then "Volcano" and so on.
…Which is a bit more of a standard looking for loop. Inside of the parenthesis in the first line are three parts (separated by a semi-colon). The first is where we initialize a variable (making i equal 0), the second part is our condition (if i is less than the number of items in levels), and the last part is the increment (i++ makes i equal itself plus 1 after each iteration).
So that for loop will run as long as i is less than levels.count. During each iteration, you can use the value of i in some way. Keep in mind, the first time around it will be 0, then 1, then 2 and so on. Which is also handy if we wanted to inspect an array with i being used as an index (which is implied in the code above).
I created a constant dictionary (using let instead of var) with "Swamp" as the first key, and 1 as the first value. Then on the next line, create a second key and value. Commas separate each pair. I chose to write each pair on separate lines, but you could write them on a single line. I think its a tad prettier this way.
So for every item in levelDifficulties, the levelName and difficulty will be something different. Again keep in mind that this is an unordered list, so the compiler might print out "I'm so scared we are at the Haunted Wal-Mart, and the difficulty is 50" the first time this runs.