DEV Community

Naveen Ragul B
Naveen Ragul B

Posted on • Edited on

Swift - Enumerations

  • An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code. enumeration definition defines a new type. example :
enum CompassPoint {
    case north
    case south
    case east
    case west
}
Enter fullscreen mode Exit fullscreen mode
  • To have a collection of all of that enumeration’s cases, by conforming to CaseIterable protocol. The compiler can automatically provide an implementation of the CaseIterable requirements for any enumeration without associated values or @available attributes on its cases.

example :

enum CompassDirection: CaseIterable {
    case north, south, east, west
}
for direction in CompassDirection.allCases {
    //body
}
Enter fullscreen mode Exit fullscreen mode

Associated Values

These are values(additional information) of any type that are stored alongside with case value. associated values can be of any type, and the value types can be different for each case. Each instance of enum can have different associated value for same type.

example :

enum Barcode {
    case upc(Int, Int, Int, Int)
    case qrCode(String)
}
var productBarcode = Barcode.upc(8, 85909, 51226, 3)
productBarcode = .qrCode("ABCDEFGHIJKLMNOP")
Enter fullscreen mode Exit fullscreen mode
switch productBarcode {
case .upc(let numberSystem, let manufacturer, let product, let check):
    print("UPC: \(numberSystem), \(manufacturer), \(product), \(check).")
case .qrCode(let productCode):
    print("QR code: \(productCode).")
}
Enter fullscreen mode Exit fullscreen mode

Raw Values

Enumeration cases can come pre-populated with default values called raw values, which are all of the same type. Each raw value must be unique within its enumeration declaration.The raw value for a particular enumeration case is always the same.

example :

enum ASCIIControlCharacter: Character {
    case tab = "\t"
    case lineFeed = "\n"
    case carriageReturn = "\r"
}
Enter fullscreen mode Exit fullscreen mode
  • For enumerations that store integer or string raw values, you don’t have to explicitly assign a raw value for each case.

example :

enum Planet: Int {
    case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
}
Enter fullscreen mode Exit fullscreen mode
  • access the raw value of an enumeration case with its rawValue property:

example :

let earthsOrder = Planet.earth.rawValue // earthsOrder is 3
Enter fullscreen mode Exit fullscreen mode
  • If you define an enumeration with a raw-value type, the enumeration automatically receives an initializer that takes a value of the raw value’s type (as a parameter called rawValue) and returns either an enumeration case or nil. You can use this initializer to try to create a new instance of the enumeration.

example :

let possiblePlanet = Planet(rawValue: 7) //optional(Planet.uranus)
Enter fullscreen mode Exit fullscreen mode

The raw value initializer is a failable initializer.

Recursive Enumeration

A recursive enumeration is an enumeration that has another instance of the enumeration as the associated value for one or more of the enumeration cases. You indicate that an enumeration case is recursive by writing indirect before it.

example :

enum ArithmeticExpression {
    case number(Int)
    indirect case addition(ArithmeticExpression, ArithmeticExpression)
    indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
}
// (5 + 4) * 2:
let five = ArithmeticExpression.number(5)
let four = ArithmeticExpression.number(4)
let sum = ArithmeticExpression.addition(five, four)
let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))
Enter fullscreen mode Exit fullscreen mode
func evaluate(_ expression: ArithmeticExpression) -> Int {
    switch expression {
    case let .number(value):
        return value
    case let .addition(left, right):
        return evaluate(left) + evaluate(right)
    case let .multiplication(left, right):
        return evaluate(left) * evaluate(right)
    }
}
print(evaluate(product)) // Prints "18"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)