Raw values
Unlike enumeration values in C, Swift enum values are not backed by integers as a default. That means january is itself the value.
But you can associate a raw value with each enumeration case simply by declaring the raw value type on the enumeration name:
enum Month: Int {
Swift enumerations are flexible; you can specify other raw value types like String, Float or Character. As in C, if you use integers and don’t specify values as you’ve done here, Swift will automatically assign the values 0, 1, 2 and up.
In this case, it would be better if January had the raw value of 1 rather than 0. To specify your own raw values, use the = assignment operator:
enum Month: Int {
case january = 1, february = 2, march = 3, april = 4, may = 5,
june = 6, july = 7, august = 8, september = 9,
october = 10, november = 11, december = 12
}
This code assigns an integer value to each enumeration case.
There’s another handy shortcut here: the compiler will automatically increment the values if you provide the first one and leave out the rest:
enum Month: Int {
case january = 1, february, march, april, may, june, july, august, september, october, november, december
}
You can use the enumeration values alone and never refer to the raw values if you don’t want to use them. But the raw values will be there behind the scenes if you ever do need them!