Working with enumerations

Extracting information from an enumeration

To use an enumeration, use the period (.) operator to reference elements of the enumeration class:
  • enumerationClassName.enumerationConstantName

To use the name of the enumeration element as a String, get its Name property. To use the zero-based index of the enumeration element as an Integer, get its Ordinal property.

Comparing enumerations

You can compare two enumerations by using the == operator. For example:

if (myFruitType == FruitType.Apple) {
  print("An apple a day keeps the doctor away.")
}
if (myFruitType == FruitType.Banana) {
  print("Watch out for banana peels.")
}
Running this code produces the following line if myFruitType references FruitType.Banana:
  • Watch out for banana peels.