Synthesised Equatable, What is it and how has it changed!

The Equatable protocol allows you to compare one type against another. When we say “Thomas” == “Thomas” we are using  Swifts Equatable protocol. This is because Strings, Numbers, Arrays and more all conform to this amazing protocol.

Making your own value types Equatable allows them to work like the Swit Standard Library properties. That is Strings, Numbers arrays and more. Saying that making your own value types Equatable can be a tad annoying. For a minute let us contemplate the following struct:

As an example, let us say that you have two Car objects and you want to check if they are identical or not. To do this you are going to need to compare every single element of the class.

So your code will need to look like this:

It’s not very Swift like, thus a change is due!

By simply adding the Equatable as a protocol for your type Swift will instantly conform to this protocol generating == methods for us :D.  Now, that’s better!

However, if you work with a backend you may find that that the only important value that you want to compare is the unique ID. That being the case, you would still need to write an equatable function like I’ve just explained above.