How to test the equality of Swift enums with associated values?


In Swift, you can use the Equatable protocol to compare enums with associated values. In this article, we will see how we can compare them using the Equatable protocol with an example.

Enumeration with Associated Values

In Swift, you can provide a value along with an enum case. This makes enumeration more powerful in Swift. A Swift feature called an enumeration with associated values enables you to design a type that can have a finite set of cases, each of which can have a unique set of associated values of any type. This enables you to link data to each of the enumeration cases.

Here is an example of creating an enumeration with associated values −

enum Temperature {
   case Celsius(Double)
   case Fahrenheit(Double)
   case Kelvin(Double)
}

The Temperature enumeration in this example has three cases: Celsius, Fahrenheit, and Kelvin. Each case has a value of type Double associated with it.

Example

Therefore, you can produce instances of the Temperature enumeration like this

let celsius = Temperature.Celsius(25.0)
let fahrenheit = Temperature.Fahrenheit(77.0)
let kelvin = Temperature.Kelvin(298.0)
print("Temperature in Celsius:", celsius)
print("Temperature in Fahrenheit:", fahrenheit)
print("Temperature in Kelvin:", kelvin)

Output

Temperature in Celsius: Celsius(25.0)
Temperature in Fahrenheit: Fahrenheit(77.0)
Temperature in Kelvin: Kelvin(298.0)

Example

This is an example of how to use associated values for enums. In the following example, the temperatureString function accepts a parameter that is an instance of the Temperature enumeration and returns a string that represents the temperature. The distinct cases of the enumeration are matched using the switch statement, and the related values are then extracted and used to build the string.

import Foundation
enum Temperature {
   case Celsius(Double)
   case Fahrenheit(Double)
   case Kelvin(Double)
}

func temperatureString(_ temperature: Temperature) -> String {
   switch temperature {
      case .Celsius(let value):
      return "\(value)°C"
      case .Fahrenheit(let value):
      return "\(value)°F"
      case .Kelvin(let value):
      return "\(value) K"
   }
}

let celsius = Temperature.Celsius(25.0)
print(temperatureString(celsius))

Output

25.0

How to Compare Them?

In the above enum, we will use the Equatable protocol to compare two enums. In this example, celsius1 and celsius2 have the same case with the same associated value "25.0", so they are considered equal. While celsius1 and celsius3, on the other hand, have the same case, but with different associated values, so they are not considered equal.

Example

import Foundation
enum Temperature: Equatable {
   case Celsius(Double)
   case Fahrenheit(Double)
   case Kelvin(Double)
}
let celsius1 = Temperature.Celsius(25.0)
let celsius2 = Temperature.Celsius(25.0)
let celsius3 = Temperature.Celsius(27.2)
if celsius1 == celsius2 {
   print("celsius1 and celsius2 are equal")
} else {
   print("celsius1 and celsius2 are not equal")
}
if celsius1 == celsius3 {
   print("celsius1 and celsius3 are equal")
} else {
   print("celsius1 and celsius3 are not equal")
}

Output

celsius1 and celsius2 are equal
celsius1 and celsius3 are not equal

Conclusion

Swift provides you with powerful enumeration through which you can associate the values with enum cases. You can define n number of cases with associated values of any type. You can associate the custom type also with an enum in Swift.

You can compare the enum instances even have associated values. For example, you want to compare two enum objects similar to custom objects. In order to compare the enum, you have to conform to the Equatable protocol at the time of the enum declaration. This protocol enables you to compare the enum objects.

Updated on: 11-Apr-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements