Enums can be confusing for everybody, especially the newbies. I mean, why use those when you can just create variables and constructs. It does the same right? Some claim that they use enum to make their code type-safe, error free and easy to use. I guess that would be true if an app is becoming perplexingly complex.

In basic form, it can look like this:

enum Directions { case north case south case east case west } // Defining multiple cases on a single line enum Directions { case north, south, east, west }

Cool, so what then? How and why would you want to use those? After calling forth Directions.north with a print , you’ll just see this in the console "north

" . You might wonder why is this even useful? It’s like calling a variable inside of a struct type.

Let’s move on to Raw Values. We now know that cases will return a string. Aside from it, it can also return a rawValue . Just how do we do that? It’s easy, just inherit from a type by doing something like this: enum Directions: Int . Now when you print( Directions.north.rawValue ), you’ll get 0 . It’s just like Directions became some sort of array and north is indexed at 0 . Here’s another example of that:

enum AnimationCurve: Int { case easeInOut, easeIn, easeOut, linear } print(AnimationCurve.easeIn.rawValue) // "1

"

The next example will not be able to inherit from types to call rawValues from cases.

enum Directions { case north(String) case south(latitude: Double, longitude: Double) case east(String) case west(String) } var digitalCompass = Directions.south(latitude: -120.83, longitude: 37.0) print(digitalCompass)

When printed, this will give a value of "south(latitude: -120.83, longitude: 37.0)

" . Neat huh? So, basically it’s a string. Cases are a f***ing string with no spaces because they are also variable names at the same time! Genius!

Wait! Hold on. Is it really a string though? No

let direction = Directions.north("town")

In this example, direction will be of type Directions not of type string.

How do we get the values inside of the parenthesis? Is there some kind of rawValue mechanism we can use for this? As a matter of fact, there is! We can print a form of string from the values inside of these parentheses. If you trust me on this, we can print out Latitude is 0.5, while longitude is 0.6 just by printing print(Directions.south(latitude: 0.5, longitude: 0.6).description) . We just have to extend this enum like it’s some kind of construct and create a computed property, we chose to use the word description as you can see. It can be anything actually.

extension Directions { var description: String { switch self { case .sorth(let direction): return direction case .south(let latitude, let longitude): return "Latitude is \(latitude), while longitude is \(longitude)" case .east(let isItReally): return "The direction is \(isItReally)" case .west(let westeros): return "King's landing is in \(westeros)" } } } print(Directions.south(latitude: 0.5, longitude: 0.6).description)

There’s another way to simply do this without having to call the computed property description directly. We can inherit this extension from CustomStringConvertible, like this:

extension Directions: CustomStringConvertible { var description: String {

, but the computed property should be description . If you use another variable name instead of it, then you will not be able to use CustomStringConvertible ’s feature.

Now all you have to do to get the same result is print out Directions.south(latitude: 0.5, longitude: 0.6) without the description after it.

That’s the idea of how to return strings out of enums with parentheses. The let latitude, let longitude are just parameter names. If you’ve noticed, it can actually be named anything too.

Now that we know how to work with enums. There are other things you can do with it too, like comparing complex enum’s value to a similar type of enum. This is a tutorial by Károly Nyisztor and it teaches just that.

Thanks to BeCodable, Károly Nyisztor, and Antoine v.d. SwiftLee 🚀. The examples come from their posts. Take your enums knowledge to the next level by clicking on their names, it links to their post about enums. Since my intention about this post is to just bridge the gap between beginners to learn the essence of enums, these guys will do you better with more examples and applications on how it really works.