Enums in Swift tie related values together in a type-safe way.
For example, instead of calling a direction "Up"
or "Down"
in your program, you should create an enum
that couples these options together:
enum Direction { case Up case Down case Left case Right }
Then you can call Direction.Up
or Direction.Down
.
A Common Use Case for Enums in Swift
Say you have an app that needs to control the direction of the game character and it can only move up, down, left, right.
Someone unfamiliar with enumerations would probably use this kind of approach:
func handleDirection(_ direction: String){ switch direction { case "Up": print("Moving up...") case "Down": print("Moving down...") case "Left": print("Moving left...") case "Right": print("Moving right...") default: print("Not supported...") } }
This function expects one of the following strings Up, Down, Left, Right
as its params. In case the input isn’t one of those, Not supported...
is printed. This works fine:
handleDirection("Up") // prints 'Moving up...' handleDirection("not a direction") // prints 'Not supported...'
Let’s try it again, though:
handleDirection("up")
The program runs and everything seems ok, but you should be moving and the console says:
Not supported...
Can you spot a mistake?
The Little Mistake
The issue is that the direction parameter should begin with an uppercase letter. In the above code, it is “up” with lowercase.
This mistake can go unnoticed easily, especially when the program is big.
The issue is there’s no clear way to be sure whether a direction is spelled correctly. You always have to come back to see the implementation of handleDirection
to be certain.
This is when enums can help. Let’s repeat the direction handling using enumerations.
First, let’s declare an enumeration for directions using the keyword enum
and creating a case
for each direction:
enum Direction { case Up case Down case Left case Right }
Instead of using mystic strings, you now have Direction
— a type that classifies the four different directions.
Now, you can pass a Direction
argument to the handleDirection
function instead of a string:
func handleDirection(_ direction: Direction) { switch direction { case Direction.Up: print("Moving up...") case Direction.Down: print("Moving down...") case Direction.Left: print("Moving left...") case Direction.Right: print("Moving right...") } }
You can now call handleDirection
this convenient way:
handleDirection(Direction.Up)
Output:
Moving up...
Better yet, calling handleDirection
with incorrect input is not possible anymore as the code does not even compile. This takes the “human error” out of the equation when it comes to typing the direction.
Additional Improvements for Using Enums in Swift
Now you know the basics of enums in Swift and how to use them.
Next, let’s improve the previous example a bit by utilizing the built-in features of enums.
1. Avoid Repetition Using Enums in Swift
You may change the handleDirection
so that you do not need to repeat Direction
in each case check:
func handleDirection(_ direction: Direction) { switch direction { case .Up: print("Moving up...") case .Down: print("Moving down...") case .Left: print("Moving left...") case .Right: print("Moving right...") } }
Instead of Direction.Up
you can write .Up
. This is because the compiler already knows the direction
argument is an enumeration with the four possible cases. Thus you do not need to specify the Direction
separately in each case
.
2. Enum Raw Values in Swift
You can associate each enum case with a raw value:
enum Direction: String { case Up = "Moving up..." case Down = "Moving down..." case Left = "Moving left..." case Right = "Moving right..." }
This way, you can replace handleDirection
function with an even shorter method:
func handleDirectionEnum(_ direction: Direction) { print(direction.rawValue) }
Conclusion
Enums in Swift lets you write type-safe code by grouping related values together.
You can create an enumeration with the enum
keyword and create a case
for each related value.
For instance, here is a direction enumeration that groups together screen directions:
enum Direction { case Up case Down case Left case Right }
Thanks for reading. Happy coding!