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

Associated values

Enumerations can store associated values of any given type, and the value types can be different for each member of the enumeration, if required. Enumerations similar to these are known as discriminated unions, tagged unions, or variants in other programming languages. The following example presents a simple usage of associated values:

enum Length {     case us(Double)     case metric(Double) } let lengthMetric = Length.metric(1.6) 

The enumeration type Length can either take a value of us with an associated value of the Double type or a value of metric with an associated value of the Double type.

The lengthMetric is a variable that gets assigned as a value of Length; metric with an associated value of 1.6.

As seen in the preceding ...

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