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

14 Enumerations

Up to this point, you have been using 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 defined 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. Define an enumeration of possible text alignments.

Listing 14.1 Defining an enumeration

import Cocoa

var str = "Hello, ...

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