14 Enumerations

As you have worked through the book up to this point, you have been using all the built-in types that Swift provides, like integers, strings, arrays, and dictionaries. The next couple of chapters will show the capabilities the language provides to create your own types. The focus of this chapter is enumerations (or enums), which allow you to create instances that are one of a predefined list of cases. If you have used enumerations in other languages, much of this chapter will be familiar. But Swift’s enums also have some advanced features that make them unique.

Basic Enumerations

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

Listing 14.1  Defining ...

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

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