Member-only story

Swift Programming Tutorial: Equatable

Learn the Concept of Equatable in No Time

Arc Sosangyo
3 min readDec 6, 2024
Photo by Tim Mossholder on Unsplash

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!”

--

--

Arc Sosangyo
Arc Sosangyo

Written by Arc Sosangyo

Arc is an iOS Dev and app publisher, a former IT manager who transitioned to iOS engineering, and a big fan of AI, coding, science, history, and philosophy.

Responses (1)