Enums are one of the basic constructs that the Swift language offers. At the same level as classes, structs, and functions, they are used to represent values that can only have a finite amount of states.
Take the Optional
enum, for example; it is represented by an enum perfectly. It represents a value that can have two, and only two, states, represented by the two members of the Optional
enum. It can either be initialized to .none
or filled with a value, .wrapped(value)
.
Enums are incredibly powerful in Swift. From very simple cases to generics, they are among the most powerful tools that we have for writing our programs.
Let's say you're building a smart light remote control; you can easily represent the state of this light with the following enum
:
enum State { case on case off } let anOnLight = State.on
This is a very simple example, and we could have used a Boolean value, but with the enum
, we set ourselves up for expansion.
Now, we may want to add a method to this State
enumeration. After all, it's very common to just toggle the switch on and off without thinking:
extension State { mutating func toggle() { self = self == .off ? .on : .off } } var state: State = .on state.toggle() state == .off // true
As in the previous section, we can just extend the State
enum to add the toggle
functionality. Enums follow value semantics; therefore, we have to mark the toggle
method as mutating.
Enums can also contain associated values. In our scenario, we can leverage this to represent a dimmer. A dimmer changes the intensity of the light, so we can represent it with a third member-the dimmed member:
enum State: Equatable { case on case off case dimmed(value: Double) }
You may have noticed that we needed to add the Equatable
conformance. This is required, as otherwise, the compiler can't synthesize equality anymore without our hint. This implementation works, but we lack a few things. First, not all Double
values are valid; we'd probably like to keep these in a reasonable span (between 0
and 1
, for example). But perhaps not all of our lights support such values between 0
and 1
. Others may want to support between 0
and a 100
or integers between 0
and 255
.
In the following example, we will build a fully generic light:
enum State<T>: Equatable where T: Equatable { case on case off case dimmed(T) } struct Bits8Dimming: Equatable { let value: Int init(_ value: Int) { assert(value > 0 && value < 256) self.value = value } } struct ZeroOneDimming: Equatable { let value: Double init(_ value: Double) { assert(value > 0 && value < 1) self.value = value } } let nostalgiaState: State<Bits8Dimming> = .dimmed(.init(10)) let otherState: State<ZeroOneDimming> = .dimmed(.init(0.4))
The dim type is now specified as a part of the State
type. This gives us a lot of flexibility, as well as validation. Wrapping the value into a small struct
adds very little overhead in terms of performance, and allows us to ensure that the values are sane before being set into our enum
.
A raw type is a base type for all enumeration members; in our example, we can hardcode presets for our dimming, as follows:
enum LightLevel: String { case quarter case half case threequarters } let state: State<LightLevel> = .dimmed(.half)
Thanks to the generic implementation and the fact that String
is equatable, we can use this raw value in our dimmed
state.
With the LightLevel
enum, which has a raw type of String
, the compiler will use the member name as the underlying raw value:
LightLevel.half.rawValue == “half” // == true
You can override these by specifying them, as follows:
enum LightLevel: String { case quarter = “1/4” case half = “1/2” case threequarters = “3/4” }
When using Int
as a raw type, the underlying raw values will follow the order of the cases:
enum Level: Int { case base // == 0 case more // == 1 case high = 100 case higher // == 101 }
With our final case, let's look at how to interpret the current state of the light:
switch state { case .on: doSomething() case .off: doSomething() case .dimmed(let value): switch value { case .quarter: doSomething() case .half: doSomething() case .threeQuarters: doSomething() } }
The switch
statement in Swift is very different from the one in Objective-C. First, the cases do not fall through each other, so there's no need to add the break
statement after each case.
If you want multiple cases to be handled with the same code, you can use the following strategy:
switch state { case .on, .off: doSomething() default: break }
Falling through is somehow not encouraged in Swift, so always try to adapt your code in order not to leverage this. If you can't avoid it, the following code shows how it should be implemented:
switch state { case .off: doSomethingOff() fallthrough case .on: doSomething() default: break }
If state
is off
, both doSomethingOff
and doSomething
will be called. If state
is on
, only doSomething
will be called.