Basic Enumerations

Create a new macOS playground called Enumerations. Define an enumeration of possible text alignments.

Listing 14.1  Defining an enumeration

import Cocoa

var str = "Hello, playground"

enum TextAlignment {
    case left
    case right
    case center

You define an enumeration with the enum keyword followed by the name of the enumeration. The opening brace ({) opens the body of the enum, and it must contain at least one case statement that declares the possible values for the enum. Here, you include three.

As an aside, the names of types (including enums) begin with a capital letter by convention. If multiple words are needed, capitalize the first letter of each word (this is called Pascal case): PascalCasedType ...

Get Swift Programming: The Big Nerd Ranch Guide, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.