Stephen Haney

Using the Power of Swift Enums

Quickly: General Enum Usage

You can use enums to create a new type in your code for use with related values. It can be a shortcut for a Class when you know the number of instances you’ll need ahead of time (you can create an enum called ‘Month’ with members like January, February, March, and so on). Typically you’d use an enum when all values can be hard coded, but Swift opens the door for some other uses. Let’s explore the new abilities.

Swift Enums are Juicy

Apple decided that Swift enums should be very flexible. You don’t have to set values on declaration, and unlike Java, values can be strings, characters, ints or floats. Also, you can use a tuple and set multiple values for each enum member. This is really powerful in that it allows you a field that accepts more than one value type. The Apple Swift Book uses UPCs as an example – you can have one Barcode type with an enum that can take either a standard UPC or a QRCode. When you use the value you can respond accordingly in your code using switch statements.

iPhone Game Example

After reading up on enums in Swift, my thoughts turned towards enum use in iOS game dev. I want to create a use case with the new functionality in Swift, so I’m going use an enum to pass gameplay status data to our loading screen. Will I really do this? Probably not – I don’t like the readability. The individual values inside the tuples aren’t named. I’d much rather use named fields for code completion support. That said, it’s a way to learn enum’s new possibilities in Swift.

// creating our enum
enum GameStatus {
    case Zoning(Int, Int)  // if we're moving between levels, we want to show current score and number of lives left
    case GameOver(Int)  // if the game's over, we'll just show the high score
    case Beginning(String)  // the game hasn't started yet - let's pass along a tip to display on the loading screen
}

We’ll pass this data into our loading screen whenever we go to it. The loading screen can display different content based on the GameStatus.

// loading screen display logic
switch gameStatus {
    case .Zoning(let currentScore, let numberOfLives):
        println("The game is moving between levels");
        // TODO: set a label to display the currentScore and numberOfLives
    case .GameOver(let lastScore):
        println("The game is over");
        // TODO: display the final score as we load back to the main menu
    case .Beginning(let tip):
        println("The game is about to begin!");
        // TODO: display the tip until the game begins!
}

This works pretty well, but there’s obviously other (better?) ways to achieve this. We can store all this data on one of our classes and simply look it up from the loading screen when needed. However, passing data in strongly typed structures may become more important when concepts like access modifiers (think private variables) are introduced into Swift. Regardless, I appreciate the flexibility of Swift enums, even if I have to reach to come up with a use case for the new features in game development. How else can we use the new enum functionality in our games?