 # Swift enum With Associated Values

In Swift enum, we learned how to define a data type that has a fixed set of related values.

However, sometimes we may want to attach additional information to enum values. These additional information attached to enum values are called associated values.

Let's see an example,

``````enum Distance {

// associate value
case km(String)
...
}``````

Here, `(String)` is additional information attached to the value `km`. It represents that the value of `km` can only be a `String`.

Now, here's how we assign the associated value to the enum value.

``Distance.km("Metric System")``

Here, `"Metric System"` is the value associated with the `km` value.

## Example: Swift enum with Associated Values

``````enum Distance {

// associate value
case km(String)
case miles(String)
}

// pass string value to km
var dist1 = Distance.km("Metric System")
print(dist1)

// pass string value to miles
var dist2 = Distance.miles("Imperial System")
print(dist2)``````

Output

```km("Metric System")
miles("Imperial System")```

In the above example, we have created an enum named Distance with the following associated values:

• `"Metric System"` is assigned to the `km` value.
• `"Imperial System"` is assigned to the `miles` value.

## Associate Multiple Values

In Swift, we can also associate multiple values to a single enum value. For example,

``````enum Marks {

// associate multiple Double values
case gpa(Double, Double, Double)

// associate multiple String values
}

// pass three Double values to gpa
var marks1 = Marks.gpa(3.6, 2.9, 3.8)
print(marks1)

// pass three string values to grade
var marks2 = Marks.grade("A", "B", "C")
print(marks2)``````

Output

```gpa(3.6, 2.9, 3.8)

Here, we have created an enum with two values: `gpa` and `grade`. Notice the enum values,

``````// associate multiple Double values
case gpa(Double, Double, Double)

// associate multiple String values

This indicates that we have to associate three `Double` and `String` values to `gpa` and `grade` respectively. These associated values are also called tuples.

So, we have attached 3.6, 2.9, 3.8 to `gpa`.

``````// pass three integer values to gpa
var marks1 = Marks.gpa(3.5, 2.9, 3.8)``````

And, we have attached `"A"`, `"B"`, `"C"` to `grade`.

``````// pass three string values to grade
var marks2 = Marks.grade("A", "B", "C")``````

## Named Associated Values

In Swift, we can also provide a name to the associated value to make our code more readable. For example,

``````enum Pizza {

case small(inches: Int)
}``````

Here, we have provided the name inches to the associated value.

It makes more sense while reading the named associated value instead of

``case small(Int)``

### Example: Named Associated Values

``````enum Pizza {

// named associated value
case small (inches: Int)
case medium (inches: Int)
case large (inches: Int)
}

// pass value using provided names
var pizza1 = Pizza.medium(inches: 12)
print(pizza1)``````

Output

`medium(inches: 12)`

In the above example, we have provided the name inches to the associated value.

While passing associated value to the case, we have used the provided name,

``Pizza.medium(inches: 12)``

## enum Associated Values and Switch Statement

We can use a Swift switch Statement to match enum associated values. For example,

``````enum Mercedes {

case sedan(height: Double)
case suv(height: Double)
}

var choice = Mercedes.suv(height: 5.4)

switch(choice) {
case let .sedan(height):
print("Height:", height)

case let .suv(height):
print("Height:", height)

print("Height:", height)
}``````

Output

`Height: 5.4`

In the above example, we have used a `switch` statement to match the enum associated values. Notice the statement,

``var choice = Mercedes.suv(height: 5.4)``

Here, we have attached 5.4 to `suv` and assigned it to the enum variable choice.

Then, we have used choice inside the `switch` statement to compare the enum case with the value of each `case` statement. Notice the switch case statement,

``case let .suv(height):``

Here, `let` binds the associated value to the height variable, which we can use in the body of the `case`.

Since the value matches with `case let .suv`, the statement inside the `case` is executed.

## Raw Values VS Associated Values

In Swift, raw values are predefined constant values provided to each enum value. For example,

``````enum Vehicle: String {

case car = "Four Wheeler"
case bike = "Two Wheeler"
}``````

Here, we have provided the raw values: `"Four Wheeler"` and `"Two Wheeler"` to `car` and `bike` respectively.

However, associated values are more like variables associated with the enum values. For example,

``````enum Marks {

case gpa(Double)
}``````

Here, `grade` and `gpa` have an associated value of `String` and `Double` type respectively.

And we assign the associated value to the enum value while creating a variable.

``var marks = Marks.grade("A")``

Notes:

1. An enum cannot have both raw values and associated values at the same time.
2. The raw values of an enum must be of the same data type. But associated values can be of any type.