Enums (Enumerations)

#enum #enumaration #case

Overview

In strings, there are many possible options for one particular item or column. For example, you may write to the name column in different ways like:

  • "Monday"

  • "Monday "

  • "monday "

  • "February"

Thus this will give erroneous results. To prevent this, you should carefully analyze your data regularly, if not every time.

You create a list of data in a particular "enum" so that you will choose from that list to prevent entering wrong data.

// First way
enum Weekday {
    case monday
    case tuesday
    case wednesday
    case thursday
    case friday
}

// Second way
enum Weekday {
    case monday, tuesday, wednesday, thursday, friday
}

// Usage
var day = Weekday.monday
day = Weekday.tuesday
day = Weekday.friday

Comparing

enum Sizes: Comparable {
    case small
    case medium
    case large
}

let first = Sizes.small
let second = Sizes.large
print(first < second) // compile: small

// Because in enum list, "small" comes before "large"

Sources

Videos

Hacking with Swift / How to create and use enums

Articles / Documents

Last updated