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 = MLSTeam.montreal
The type of theTeam
is inferred when it is initialized with MLSTeam.montreal
. As theTeam
is already defined, we can change it with a shorter syntax as follows:
theTeam = .newYork
In Swift, enums are very powerful...