-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Swift 4 Protocol-Oriented Programming - Third Edition
By :
In most languages, enumerations are little more than a data type consisting of a set of named values called elements. In Swift, however, enumerations have been supercharged to give them significantly more power. Enumerations in Swift are a lot closer in functionality to classes and structures; however, they can still be used like enumerations in other languages.
Before we see how enumerations are supercharged in Swift, let's see how we can use them as standard enumerations. The following code defines an enumeration called Devices:
enum Devices
{
case IPod
case IPhone
case IPad
} In the Devices enumeration, we defined three possible values: IPod, IPhone, and IPad. One of the reasons why enumerations are different in Swift as compared to other languages is that they can be prepopulated with values known as raw values. As shown in the following example, we could redefine our Devices enumeration to be prepopulated with String values:
enum Devices: String {
case IPod...