#Swift Enum Mirego
Explore tagged Tumblr posts
Text
Invoking the power of Enums

By: Romain Pouclet
The past few months have been pretty exciting for iOS developers. While some frustration about Objective-C was starting to boil more and more, Apple came with a new programming language called Swift. At a time when developers started having this weird dream of letting the language and the compiler do the boring work so they could focus on implementing features adding values and creating nice UI, you couldn’t have dreamt for a better timing. Today, I’m going to talk about one of the many reasons that make you love Swift: enums.
An enumerated type (or enum) is a set of named values. When a variable is declared as having an enumerated type, it can only contain one of those values. A very simple example would be a classical card game where a card can either be of clubs, spades, diamonds or hearts. When it would be time to play a new round, using an enum would ensure that let’s say only 3 of the spades, diamonds, hearts or clubs could be played. A lot less safe versions of this would be having a list of 4 “available types” for your cards. In your code, you would then send the “index” of the type of the card being played: 0 being “Spade”, 1 being Diamonds… It’s not really pretty but it would probably do the job! What happens if you encounter a bug in your codebase though? What if somehow the index of the type was swipped to -1 or 17? That’s a lot of not-so-edgy-cases you’d have to think about that Swift would take care of for you. A card would have a number and type. A type is either Spades, Diamonds, Hearts or Clubs. Period.
In Swift, the code would look like this:
enum CardType { case Spades case Diamonds case Hearts case Clubs } struct Card { let number: Int let type: CardType }
You would then create a new card using the following code:
let card = Card(number: 3, type: .Spades)
Of course, this is the kind of example that you would find in programming book (and not a very good one!) An actual game of cards would imply a lot more rules than that (what happens if I want a Queen? A King? Can I have multiple 3 of Spades..?).
Unless you’ve found a way to break the rules and submit a gambling app on the App Store, chances are this won’t be of any use. Instead, we had the idea of this article when we refactored an application with a timer feature. By pressing on a button, a screen would appear, letting you choose a duration for setting a new timer or clearing the existing timer. Taping anywhere on the screen or on the close button would dismiss this screen. This means at the end of the action, you’ll have 3 possible results to handle in your application:
The user decided to start a timer of a specific duration
The user simply decided to clear the current timer
The user cancelled the action by tapping somewhere in the screen
These 3 cases can be expressed as an enum the following way:
enum TimerAction { case Add(Int) case ClearCurrent case Dismiss }
What can get extremely powerful with Swift enums is being able to add extra informations for each and any value of this enumerated type. In this case, we want the duration selected to set the timer:
func didSelectAction(action: Action) { switch(action) { case .Add(let duration): print(“User created a timer for \(duration) minutes") case .ClearCurrent: print(“User want to clear the current timer") case .Dismiss: print(“Dismiss the current screen") }
}
Swift comes with a great deal of awesome features like this one and enums can be used to implement a lot more of different concepts. If you’re curious about how else you could use them in your application, the [official documentation] would be a good place to start.
Want to learn about Continuous intergration for iOS? Check out Romain’s blog post.
0 notes