
Here I’m going to try and explain what’s this Equatable protocol in Swift. First, let’s go to the basics.
Whenever we try to compare two things like numbers or strings, we use == or != , for example we say 2 == 2 or 2 != 3
Numbers in Swift follow the Equatable protocol, that’s why Swift knows how to treat them when comparing, so, the next question would be..
Equatable?Think of Equatable as a rulebook that tells Swift how to compare things to see if they are equal or not equal. When something like a Int, String or your custom object, follows the Equatable rulebook, Swift uses the == or != to compare those things. So you don’t need more code to tell Swift how to treat those objects.
In everyday life we often compare things:
That also happens when we code, there are always things to compare when building our features in code, when our code becomes more and more complex, we need to start using other approaches to make your life easier.
When creating your own object, we can have something like this
struct Person {
var name: String
var age: Int
}
Now, when we try to compare those objects we are going to try and use == , and we get an error.

That’s why Swift doesn’t know yet how to treat these objects when comparing them, so we need to make our Person object conforms the Equatable protocol.
struct Person: Equatable {
var name: String
var age: Int
}
let person1 = Person(name: "Robert", age: 22)
let person2 = Person(name: "Robert", age: 22)
print(person1 == person2)
When doing this comparison, it is going to return true because name and age are the same.