Learn iOS with Examples, Code Snippets and Video Tutorials

Swift Basics – Part 5 – Enumerations and Switch Statements

Swift Basics – Part 5 – Enumerations and Switch Statements
October 15, 2014 Justin

In this Swift video tutorial, we look at enumerations and switch statements.

What are enumerations?  Imagine creating a bunch of constant variables all at once, in a kind of group.

What are switch statements? Well they are like if…else if statements without all the brackets.

Here’s the first snippet of code we play around with in the video…

Above you can think of DirectionMessage as the group name, and North, South, East and West as members of the group. Member is actually the proper term, by the way.

Notice that DirectionMessage has a type of String. We could go without a type at all, but since our enum members actually have a string value, we need to set the type.

In the final line we combine together “You are heading” with the rawValue of the enumeration member. By specifying rawValue it returns the actual value of the member.   Note that in Xcode 6, toRaw() would have been used here, but in Xcode 6.1, it has been changed to rawValue.

In the next example, pasted below, we do not give the enum a type like String, and instead we’ll just be happy using the members as values for a var called currentDirection. We don’t actually need to know what the rawValue is behind the member (there is none anyway), because all we care to know is which of those members currentDirection equals at a specific time. For example does it equal .NorthWest, and if so, we do something. We’ll test this with a switch statement instead of an if statement though.

Our switch statement looks at the currentDirection and tests for each possible condition. Notice we have…

… but you might have been expecting…

…Both are valid, but we can simply write .NorthEast.

The switch statement will want us to test all possible conditions of an enumerator, but leaves us the option to exclude some, and instead write…

So if currentDirection is not equal to any of the case values, then the default code is executed. In this case, we simply use break to “break out of” the switch statement entirely. You could though write lines of code here as well.

As mentioned in the video tutorial, switch statements are a great place to include functions, to call large blocks of code to execute based on the conditions. This makes things a lot prettier and easier to read. For example, you might have a case like this…

…where handleNorthEasternTravel is a function that does everything related to changing conditions for a game character going NorthEast. That might be updating the game map, changing the vector of the character, outputting different messages (like the one in our first example) and so on.

Don’t be afraid to create hundreds of functions if needed.  On a large project, its much easier to search for functions than it is for snippets of code inside of switch or if statements. Plus functions give you the flexibility of calling them in multiples places in your class.

Continue onto Part 6
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

Your email address will not be published. Required fields are marked *


Check out our premium Swift and Sprite Kit Tutorial content!

Visit CartoonSmart.com