Learn

One of the powerful functionalities Swift adds to enums is the ability to store an associated value of any type attached to a case value. This is different from raw values because each case can have a different type of associated value or no associated value at all.

To show how we can define an enum with an associated value, let’s start with this enum describing desserts:

enum Dessert { case cake case vanillaIceCream case brownie }

We might have some business logic that needs to know how many scoops of ice cream we are serving or the particular flavor of the cake. We can define associated values for the Dessert enum using the following syntax:

enum Dessert { case cake(flavor: String) case vanillaIceCream(scoops: Int) case brownie }

To instantiate an enum with an associated value we use the following syntax:

let tonightsSpecial = Dessert.vanillaIceCream(scoops: 4)

To access the associated values, we can extract them in a switch statement

switch tonightsSpecial { case let .cake(cakeFlavor): print("Time for \(cakeFlavor) cake") case let .vanillaIceCream(scoopCount): print("\(scoopCount) scoops of vanilla ice cream") case .brownie: print("Decadent goodness") } // Prints: 4 scoops of vanilla ice cream

By using the let syntax in parenthesis after the switch case statement we extract the associated value as a constant.

If an enumeration has a raw value, its cases can’t have associated values. This is because it wouldn’t be able to recreate both the case and the associated value from a single input.

Let’s add an associated value to our Vehicle enum ✈️

Instructions

1.

Add an associated Bool value called isFourWheelDrive to the truck case in Vehicle.

2.

Set isFourWheelDrive to true in the declaration of myRide.

3.

Extract isFourWheelDrive as a constant in the truck case within the switch statement.

4.

Add logic to the truck case that prints "Let's go to the mountains" if isFourWheelDrive is true, and "Let's go to the store" if it is false.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?