Equatable and Comparable in Swift

Needone App
3 min readDec 13, 2022

--

It is quite common that we are required to compare two custom objects in Swift, luckily Swift has provided some built-in protocol allows us to do this quickly.

Equatable

In Swift, Equatable is a protocol that allows you to determine whether two objects are equal. To conform to this protocol, a type must implement the == operator, which defines what it means for two objects of that type to be considered equal.

class Car {
let make: String
let model: String
let year: String?

init(_ make: String, _ model: String, _ year: String? = nil) {
self.make = make
self.model = model
self.year = year
}
}

To check two Car objects are same, we can let it comfort to Equatable protocol:

extension Car: Equatable {
static func == (lhs: Car, rhs: Car) -> Bool {
return
lhs.make == rhs.make &&
lhs.model == rhs.model &&
lhs.year == rhs.year
}
}

Then we can check whether tow cars are same in this way:

let car1 = Car("Lexus", "NX200T", "2010")
let car2 = Car("Lexus", "NX200T", "2010")
print(car1 == car2) // true

Comparable

With Equatable, we can check whether two objects are same, but we don't know which one is "bigger" or "smaller". For example, we might need to sort an array with custom objects. Similar to Equatable we have Comparable for such scenarios.

With Equatable, we can compare two objects, but we don't know which one is bigger. For example, we might need to sort an array with custom objects. Similar to Equatable we have Comparable for such scenarios.

extension Car: Comparable {
static func < (lhs: Car, rhs: Car) -> Bool {
if let _lYear = lhs.year, let _rYear = rhs.year {
return _lYear < _rYear
} else {
return lhs.make < rhs.make
}
}

static func == (lhs: Car, rhs: Car) -> Bool {
lhs.make == rhs.make && lhs.model == rhs.model && lhs.year == rhs.year
}
}

We can just need to implement < and ==, as the rest others can be derived from the given two.

Unlike Equatable, the compiler does not automatically conforms to Comparable protocol.

Summary

In Swift, Comparable and Equatable are two protocols that define a set of methods that a type must implement in order to be able to be compared or tested for equality.

A type that conforms to the Comparable protocol must implement the < and == operators, which are used to compare two values of the same type. This allows the type to be used in comparison operations, such as in if statements or other places where a comparison is needed.

The Equatable protocol is similar, but it only requires that the == operator be implemented. This allows two values of the same type to be tested for equality.

In general, if you want to be able to compare two values of a custom type, you would implement both the Comparable and Equatable protocols. This allows the values to be compared using the < and == operators, as well as other comparison operators that may be defined for the type.

Note that we can let either class or struct to comfort to Equatable and Comparable

Originally published at https://needone.app on December 13, 2022.

--

--

No responses yet