Enums in Swift

Press the switch on or off

Image for post
Image for post

enums let you define a type for a group of related values, so you can work with those values without using something awful (like Strings) that would keep you awake at nights. enums can be thought of as structs with the ability to catagorize.

Prerequisites:

  • Be able to produce a “Hello, World!” iOS application (guide HERE)
  • It would be useful to be familiar with Properties in Swift (guide HERE)

Terminology

Associated Values: Extra data that can be stored with enums to represent complex data

enum: A type consisting of a set of named values, called members

Stored Property: A constant or variable that is stored as part of a class or a struct

Type: This is the kind (type!) of value that a variable or constant can store

Simple implementations

This is not meant to be an introductory guide as the Swift programming guide https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html really covers most of the simple uses of enums. However, it makes sense to start from somewhere sensible so imagine you want a case where you can buy or sell stock.

As in the code above, it is very easy to compare enums (the true and false case)

We can certainly improve this…

enums with associated values

The stock and the amount can be associated to the trade, so we can have associated types:

It would be sensible to associate stock and an amount to the sell case too. Oh, and you can also specify default arguments for enum values in the same way that functions can have default values.

Comparing enums with associated values

In any case, it can be tricky to return the stock and amount. One way is just to print the trade as above, but what if we want to act on the amount of the stock?

Returns trade buy(stock: “Apple”, amount: 4) stock: Apple trade amount: 4

As we can see, the default component of the switch statement can be quite confusing. An alternative is as follows:

Returns trade buy(stock: “Apple”, amount: 4) stock: Apple trade amount: 4

Nested Enums

Nested types are often used in Swift. There is no reason why we can’t use nested enums, and in fact this can be done as follows:

Methods on enum types

enums are just a type. So methods can easily be attached much like you would for structs and classes.

But there is more: if you instrucce a stored instance property like let introduced: Int you might see this warning:

Image for post
Image for post

enums have type properties but not stored instance properties as a design choice. enums store data by having multiple cases, each of which can have associated values. Having stored properties on enums would make them look much like structs, and perhaps even mean that structs would not be useful any longer.

Incidentally static methods are possible but will be called on the name of the type rather than a specific instance of the same.

Mutating functions

Like structs, enums can have mutating functions. We can leverage the idea that comparing enums without associated values is easy to create a light switch

Limitations

Enums can’t have stored properties: :(

Conclusion:

Enums (short for enumeration)are great at providing collections of values where you can only take one out of a small set of possible values. Something like currency accepted at a particular airport would be suited to enums.

By using enum instead of Integers or Strings can increate compile-time checking which helps avoid errors! Do be careful though, an overuse of enums can indicate that there are too many responsibilities in single lessons.

As ever, it is down to you as the programmer to decide what and how to solve the problem that you have.

The Twitter contact:

Any questions? You can get in touch with me here

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store