iOS Protocols : Equatable | Comparable

Lovekesh Bhagat
3 min readFeb 19, 2018

--

iOS is moving towards a protocol-oriented programming since WWDC 2015 where apple mentioned about it and since then Apple has introduced different protocols for us to use, which makes our tasks easier and convenient.While working I came across many iOS protocols which can be used for different tasks in this article I am going to discuss two of them with their use. Equitable and Comparable.

Equatable

Equatable protocol introduced by apple to provide us with the functionality to check the equality between two instances of a class, with the help of equatable protocol we can use “==” and “!=” operators.The class which is needed to be compared should conform this protocol.Below is the example for it.

Suppose we created a structure, named student

struct Student {   let rollNumber:Int?    let name:String?   init(rollNumber:Int,name:String) {       self.rollNumber = rollNumber       self.name = name    } }

and we have created two instances of student

let robin:Student = Student(rollNumber:1, name:"Robin")let lovekesh:Student = Student(rollNumber:1, name:"Lovekesh")

and we want to compared these two instances

if(robin == lovekesh) {
print("same instances")
}

But in this case Xcode will show an error

Error message

So compiler is not understanding what we are doing to resolve this problem we need to inherit and conform Equatable protocol and conforming equatable means overriding the “==” operator, below is the example

struct Student:Equatable {

static func ==(lhs: Student, rhs: Student) -> Bool {

if lhs.rollNumber == rhs.rollNumber {
return true
} else {
return false
}
}
let rollNumber:Int?
let name:String?
init(rollNumber:Int,name:String) {
self.rollNumber = rollNumber
self.name = name
}
}

Now we can compare the two instances, but important thing is that they should be equatable on the basis of some property and in above example it is done using roll number.Now we can try to equate the two instances.

if(robin == lovekesh) {
print("same roll numbers")
}

After conforming equatable protocol we can use “==” and “!=” operators on custom classes objects.

Comparable

This is an another protocol provided by iOS, which provide us the functionality to use <, <=, >=, and > operators on the custom class objects, it is similar to equatable protocol i.e. we need to conform it and implement it’s methods, one more important thing about comparable protocol is that is it inherited from equatable so it must override “==” operator as well, so we need to override “==” and “<” operators.Below is the example for it.

static func <(lhs: Student, rhs: Student) -> Bool {  if lhs.rollNumber! < rhs.rollNumber!{
return true
} else {
return false
}
}
static func ==(lhs: Student, rhs: Student) -> Bool {
if lhs.rollNumber == rhs.rollNumber {
return true
} else {
return false
}
}
}

Above two methods are necessary to implement in the struct or class where it needs to be conformed, as soon as we implement these methods we can easily compare instances of respective struct or class

One more interesting fact about comparable protocol, if we create an array of Student which is using comparable protocol, we can use sort function on that array directly without mentioning anything.Below is the example

let robin:Student = Student(rollNumber:1, name:"Robin")let lovekesh:Student = Student(rollNumber:0, name:"Lovekesh")var studentArr:[Student] = [robin,lovekesh]studentArr.sort()print(studentArr)

In above ways we can use these protocols.Please give it a try.I will be coming up with some more swift protocols soon.Thanks for reading this article.

--

--