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
        default:
            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.


Thanks!


Share this article



Written By

Generic placeholder image

Gurjit Singh

I’m Computer Science graduate and CompTIA Security+ certified SOC Analyst and Mobile Application Security Engineer with 10+ years of cross-platform development experience across iOS, Android, and web.


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


MobSF Audit of an iOS app powered by the Flickr API

Static analysis of an iOS app using MobSF, identifying credential misuse, security misconfigurations, and privacy issues....

2026-05-29 . 4 min read     MobSF SAST

Read More »

Understanding Certificates in Cybersecurity

Transitioning from software development into cybersecurity is one of the most natural career moves in tech today....

2026-05-26 . 4 min read     Security Certificates

Read More »

Swift 6.1: New Features & Enhancements

Swift 6.1, officially released in March 2025, continues the evolution of Apple's powerful and expressive programming language....

2025-08-12 . 3 min read     Swift 6.1

Read More »

String Concatenation in Swift: A Comprehensive Guide

In any programming language, working with strings is essential, and Swift is no different.Whether you are building iOS apps......

2024-10-17 . 3 min read     String Concatenation

Read More »

Integrating SwiftUI with UIKit Using UIHostingController

With the introduction of SwiftUI, Apple has provided developers with a modern way to build user interfaces across all Apple platforms....

2024-07-09 . 3 min read     UIHostingController

Read More »

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 »