Equatable Vs Comparable Protocols in Swift


The Equatable and Comparable protocols are used for comparing values, but they serve different purposes.
Equatable Protocol
It is used to define equality between instances of a particular type. It requires the implementation of the ==
operator, which takes two instances of the same type and returns a boolean value indicating whether they are equal or not. The !=
operator is also provided by default for types conforming to Equatable. For example:
struct MediaAsset: Equatable {
let name: String
let duration: Double
static func == (lhs: MediaAsset, rhs: MediaAsset) -> Bool {
return lhs.name == rhs.name && lhs.duration == rhs.duration
}
}
let video = MediaAsset(name: "VideoReel", duration: 30.0)
let audio = MediaAsset(name: "SampleAudio", duration: 25.0)
let video2 = MediaAsset(name: "VideoReel", duration: 30.0)
print(video == audio) // false
print(video == video2) // true
In this example, the MediaAsset
struct conforms to the Equatable protocol by implementing the ==
operator. Two MediaAsset
instances are considered equal if they have the same name
and duration
. You can customise the comparison of values in this static method as per requirement.
Comparable Protocol
It is used for defining an order between instances of a particular type. It requires the implementation of the <
operator, which takes two instances of the same type and returns a boolean value indicating whether the first instance is less than the second. The <=
, >
, and >=
operators are also provided by default for types conforming to Comparable. For example:
struct MediaAsset: Comparable {
let name: String
let duration: Double
static func == (lhs: MediaAsset, rhs: MediaAsset) -> Bool {
return lhs.name == rhs.name && lhs.duration == rhs.duration
}
static func < (lhs: MediaAsset, rhs: MediaAsset) -> Bool {
return lhs.duration < rhs.duration
}
}
let video = MediaAsset(name: "VideoReel", duration: 30.0)
let audio = MediaAsset(name: "SampleAudio", duration: 25.0)
let video2 = MediaAsset(name: "ShortReel", duration: 16.0)
let sortedMedia = [video, audio, video2].sorted()
print(sortedMedia.map { $0.name }) // ["ShortReel", "SampleAudio", "VideoReel"]
The sorted() method is available for types conforming to Comparable, allowing the array of MediaAsset
instances to be sorted in ascending order based on the <
implementation.
Conclusion
The key difference between Equatable and Comparable is that Equatable is used to check for equality between instances, while Comparable is used to define an order or sorting criteria between instances.
Both protocols serve different purposes and can be used together if needed. For example, a type can conform to both Equatable and Comparable protocols to allow for equality checks and sorting operations on instances of that type.
Start your preparation for iOS interviews with “iOS Interview Handbook“, includes:
✅ 290+ top interview Q&A covering a wide range of topics
✅ 520+ pages with examples in most of the questions
✅ Expert guidance and a roadmap for interview preparation
✅ Exclusive access to a personalised 1:1 session for doubts
Thank you for taking the time to read this article! If you found it helpful, don’t forget to like, share, and leave a comment with your thoughts or feedback. Your support means a lot!
Keep reading,
— Swiftable
Subscribe to my newsletter
Read articles from Nitin directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Nitin
Nitin
Lead Software Engineer, Technical Writer, Book Author, A Mentor