Book Image

Mastering Swift

By : Jon Hoffman
Book Image

Mastering Swift

By: Jon Hoffman

Overview of this book

Table of Contents (22 chapters)
Mastering Swift
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Enumerations


Enumerations (or otherwise known as enum) are a special data type that enables us to group-related types together and use them in a type safe manner. For those of us who are familiar with enums from other languages, such as C or Java, enums in Swift are not tied to integer values. The actual value of an enum (known as the raw value) can be a String, a Character, an Integer or a Floating-point value. Enums also support features that are traditionally supported only by classes such as computed properties and instance methods. We will discuss these advanced features in depth in classes and structures chapter. In this section, we will look at the traditional enum features.

We will define an enum that contains the list of planets like this:

enum Planets {
  case Mercury
  case Venus
  case Earth
  case Mars
  case Jupiter
  case Saturn
  case Uranus
  case Neptune
}

The values defined in an enum are considered to be the member values (or simply the members) of the enum. In most cases...