Learn iOS SpriteKit with Examples, Code Snippets and Video Tutorials

Swift Basics – Part 6 – Arrays, Dictionaries and For Loops

Swift Basics – Part 6 – Arrays, Dictionaries and For Loops
October 15, 2014 Justin

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.

We could have also written…

…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.

For Loop with an Array in Swift

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.

A More Traditional For Loop

You can also write a for loop like this…

…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).


Dictionaries are similar to arrays, but hold a key and value instead of just a value. Dictionaries are also unordered collections. So items are intended to be “looked up” using a key, like you would searching an actual dictionary by finding the definition of a word by looking up the term.  The reason dictionaries are unordered is because if you iterate through them, you can’t count on the first item you defined as being the first item in the iteration.  Let’s have a look…

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.

Now if I wanted to find out the value of a key, lets assume that’s the level difficulty, I could write…

And instead of hard-coding a string like “Volcano” as the key, I could use a variable. For example…

For Loop with a Dictionary in Swift 

Iterating through dictionaries is basically the same. This time though we can create two variables that will change each iteration, in this case they are levelName and difficulty…

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.


Continue to Part 7
Justin Dike is the owner and lead developer at CartoonSmart.com. He's taught hundreds of hours of game development video tutorials, covering everything from coding to art. These days he's working mostly on Swift and Sprite Kit tutorials, which often lead into highly polished iOS / tvOS Starter Kits, which require no programming at all, but are capable of making nearly any type game! Yeah, you read that write. You can also find Justin at the official CartoonSmart Podcast


Leave a reply

Check out our premium Swift and Sprite Kit Tutorial content!

Visit CartoonSmart.com