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…
1 2 3 4 5 6 7 8 9 10 11 12 |
enum DirectionMessage:String { case North = "toward the Village" case South = "toward Mordor" case East = "toward Rivendale" case West = "where those elves were from" } var message:String = "You are heading " + DirectionMessage.South.rawValue //the message would be "You are heading toward Mordor" |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
enum DirectionGroup2 { case NorthEast, NorthWest, SouthEast, SouthWest } var currentDirection = DirectionGroup2.NorthWest switch (currentDirection) { case .NorthEast: println("travelling NE") case .NorthWest: println("travelling NW") case .SouthEast: println("travelling SE") case .SouthWest: println("travelling SW") default: break } |
Our switch statement looks at the currentDirection and tests for each possible condition. Notice we have…
1 2 |
case .NorthEast: println("travelling NE") |
… but you might have been expecting…
1 2 |
case DirectionGroup2.NorthEast: println("travelling NE") |
…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…
1 2 |
default: break |
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…
1 2 |
case .NorthEast: handleNorthEasternTravel() |
…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 60 Comments
Leave a reply
You must be logged in to post a comment.