O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

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

Defining enumerations

In Swift, an enumeration defines a common type for related values and enables us to work with those values in a type-safe way. Values provided for each enumeration member can be a String, Character, Integer, or any floating-point type.

The following example presents a simple definition of an enum:

enum MLSTeam {     case montreal     case toronto     case newYork     case columbus     case losAngeles     case seattle } let theTeam = MLSTeam.montreal 

enum MLSTeam provides options for MLS teams. We can choose only one of the options each time; in our example, montreal is chosen.

Multiple cases can be defined and separated by a comma on a single line:

enum MLSTeam {     case montreal, toronto, newYork, columbus, lA, seattle } var theTeam ...

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