O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Raw Value Enumerations

If you have used enumerations in a language like C or C++, you may be surprised to learn that Swift enums do not have an underlying integer type. You can, however, choose to get the same behavior by using what Swift refers to as a raw value. To use Int raw values for your text alignment enumeration, change the declaration of the enum.

Listing 14.11 Using raw values

...
enum TextAlignment: Int {
    case left
    case right
    case center
    case justify
}
...

Specifying a raw value type for TextAlignment gives a distinct raw value of that type (Int) to each case. The default behavior for integral raw values is that the first case gets raw value 0, the next case gets raw value 1, and so on. Confirm this by printing ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required