Member-only story
Swift Programming Tutorial: Equatable
Learn the Concept of Equatable in No Time
So, you’re cruising along in your Swift programming journey when you stumble upon something called the Equatable protocol. “Equatable?” you ask yourself, “Is this about tables? Equal tables? Are we building IKEA furniture now?” Nope. Fear not, my coding friend — Equatable isn’t about flat-pack furniture. It’s about making sure your custom types can check if they’re, well, equal.
Let’s break it down like a pizza party (because who doesn’t love pizza analogies? Or real pizza?).
This article is part of my Swift Programming Tutorial series.
What is Equatable Anyway?
Imagine you’re comparing two slices of pizza. Are they the same? You might check the toppings: pepperoni, mushrooms, pineapple (yes, I said pineapple — don’t @ me). In Swift, when you want to compare two instances of a type to see if they’re “equal,” you use Equatable.
Equatable is a protocol that lets you define what “equal” means for your type. By conforming to this protocol, you allow your custom types — like classes, structs, and enums — to play nice with the ==
operator. Swift will stop scratching its head and say, “Oh, I get it. These two instances are (or aren’t) the same!”