Swift Enums and Equatability: With and Without Associated Values

Swift Enums

Posted on 24 Jan 2024 . 3 min read

Swift enums provide a powerful way to model a set of related values. Enums can be equipped with associated values, allowing them to represent a variety of data structures.

In Swift, the Equatable protocol enables instances of a type to be compared for equality. Let's explore how enums behave with and without associated values when conforming to the Equatable protocol.

Swift Enum without Associated Values

Enums without associated values are simpler and automatically conform to the Equatable protocol, as long as they have no raw values.

Here’s an example:

enum Direction {
    case north
    case south
    case east
    case west

let direction1 = Direction.north
let direction2 = Direction.south
let direction3 = Direction.north

// Enum cases without associated values are not equal
if direction1 == direction2 {
    print("Both directions are equal")
} else {
    print("Directions are not equal")

// Enum cases without associated values are equal
if direction1 == direction3 {
    print("Directions are equal")
} else {
    print("Directions are not equal")

In this case, the Direction enum is equatable by default because it has no associated values. The equality comparison is based solely on the enum case.

Swift Enum with Associated Values

An enum with associated values is a versatile construct that allows you to associate additional data with each case. When dealing with equatability, both the enum and its associated values must conform to the Equatable protocol.

Consider the following example:

enum Measurement: Equatable {
    case distance(Double)
    case temperature(Double)
    case speed(Double)

let measurement1 = Measurement.distance(20.0)
let measurement2 = Measurement.distance(20.0)
let measurement3 = Measurement.temperature(40.0)

// Enum cases with associated values are equatable
if measurement1 == measurement2 {
    print("Both measurements are equal")
} else {
    print("Measurements are not equal")

// Enum cases with different associated values are not equal
if measurement1 == measurement3 {
    print("Measurements are equal")
} else {
    print("Measurements are not equal")

In this example, instances of the Measurement enum are equatable because the associated values (in this case, Double values) are equatable. The equality comparison takes into account both the enum case and the associated value.

Custom Equatable implementation

However, there are some cases where you may need to write your own Equatable conformance for an enum. This is typically the case if you have an enum with associated values that are not Equatable themselves.

In this case, you will need to implement the Equatable protocol yourself and provide a custom equality comparison function.

Here is an example of how to make an enum Equatable with associated values:

enum Fruit: Equatable {
    case apple(color: String)
    case banana(length: Double)
    case orange(diameter: Double)

extension Fruit {
    static func == (lhs: Fruit, rhs: Fruit) -> Bool {
        switch (lhs, rhs) {
        case (.apple(let lhsColor), .apple(let rhsColor)):
            return lhsColor == rhsColor
        case (.banana(let lhsLength), .banana(let rhsLength)):
            return lhsLength == rhsLength
        case (.orange(let lhsDiameter), .orange(let rhsDiameter)):
            return lhsDiameter == rhsDiameter
            return false

let fruit1 = Fruit.apple(color: "Red")
let fruit2 = Fruit.apple(color: "Green")

if fruit1 == fruit2 {
    print("Both fruits are equal")
} else {
    print("Fruits are not equal")

In this example, the Equatable conformance is implemented by providing a custom equality comparison function that checks the equality of the associated values for each case of the enum.

The bottom line

Swift enums, whether with or without associated values, can seamlessly conform to the Equatable protocol. When dealing with enums with associated values, ensure that the associated values themselves also conform to Equatable.

This capability provides a convenient way to compare instances of enums in a manner that suits the specific needs of your application. Whether your enum represents simple cases or complex data structures, Swift's type-safety and equatability support contribute to writing clean and robust code.

Don’t hesitate to contact me if you have any questions or queries. Follow me on twitter @gurjitpt for any updates.


Share this article

Written By

Generic placeholder image

Gurjit Singh

I’m Computer Science graduate and an iOS Engineer who writes about Swift and iOS development. Follow me for more updates:

Discover articles by topics

SwiftUI Class Struct Networking XCode NSCache Enum Optionals Property Observers Closures Guard Reviews StoreKit App Store Algorithms Testing Operators Protocol Extensions Weak Unowned SwiftData WWDC23 GCD API Admob SwiftLint Lottie Foreach Objective-C UIKit NavigationSplitView

Related Articles

Deep Dive into Autorelease Pools in Swift

In the realm of software development, memory management plays a crucial role in ensuring the efficient allocation and deallocation of memory...

2024-01-28 . 4 min read     Swift Autorelease

Read More »

How to create Date Picker in SwiftUI

Use a DatePicker when creating a view that enables users to choose both a calendar date and, if needed, a specific time.In SwiftUI, you can ...

2024-01-16 . 2 min read     SwiftUI DatePicker

Read More »

Getting started with Swiftlint to enforce Swift style

SwiftLint is a tool that ensures Swift code adheres to defined style guidelines. It automates code review by identifying and suggesting impr...

2023-12-29 . 4 min read     Swift SwiftLint

Read More »

How to use Lottie animation in SwiftUI

Lottie is a fantastic tool for incorporating high-quality animations into your SwiftUI projects. There are several ways to add Lottie to pro...

2023-12-13 . 2 min read     SwiftUI Lottie

Read More »

How to get index in Foreach in SwiftUI

In SwiftUI, the ForEach is used to iterate over a collection of data and create views dynamically based on that data....

2023-12-07 . 3 min read     SwiftUI ForEach

Read More »

How to implement navigation split view in SwiftUI

n SwiftUI, the NavigationView and SplitView allow you to create split-screen layouts, especially useful for iPad applications....

2023-11-30 . 2 min read     NavigationSplitView

Read More »