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.
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.
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.
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.
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!
Written By
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
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
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
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
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
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
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