How to Use If Case Let in Swift Without Losing Your Mind

Mark Moeykens
3 min readMar 8, 2023

--

Swift “if case let” is a way of writing code that makes it easier to check for something special.

For example, if you have a box of chocolates and you want to see if there is a caramel one inside, you can use “if case let” to do that.

Let me show you how it works with some code examples

First, we need to define what a chocolate is.

A chocolate can have different fillings, like caramel, nougat or mint.

We can use an enum to represent these fillings:

enum Filling {
case caramel
case nougat
case mint
}

Now we can make a struct called Chocolate that has a filling property:

struct Chocolate {
var filling: Filling
}

Next, we need to make a box of chocolates.

We can use an array to store them:

var boxOfChocolates: [Chocolate] = [
Chocolate(filling: .caramel),
Chocolate(filling: .nougat),
Chocolate(filling: .mint),
Chocolate(filling: .caramel)
]

Now we are ready to use “if case let” to find the caramel chocolates.

Here’s what it looks like:


for chocolate in boxOfChocolates {
if case let .caramel = chocolate.filling {
print(“Yum! A caramel chocolate!”)
}
}

What does this code do?

It loops through each chocolate in the box and checks if its filling is caramel.

If it is, it prints a message saying “Yum! A caramel chocolate!”.

If not, it does nothing.

It’s Like…

The “if case let” part is like saying “if this thing matches this pattern, then do something with it”.

The pattern here is “.caramel”, which means any filling that is equal to caramel.

The thing here is “chocolate.filling”, which means the filling of each chocolate in the box.

Let Me Call You Something Else…

The “let” part is like saying “let me call this thing something else”.

In this case, we don’t need to call it anything else because we already have a name for it: “chocolate.filling”.

But sometimes we might want to give it a new name so we can use it later.

For example, suppose we want to print the index of each caramel chocolate as well as the message.

We can use “let” to give the index a new name like this:

for (index, chocolate) in boxOfChocolates.enumerated() {
if case let (i, .caramel) = (index, chocolate.filling) {
print(“Yum! A caramel chocolate at index \(i)!”)
}
}

Here we are using “(index, chocolate.filling)” as the thing and “(i, .caramel)” as the pattern.

The pattern has two parts: an i and a .caramel.

  • The i matches any index and gives it a new name i.
  • The .caramel matches any filling that is equal to caramel.

So when we find a match, we can print both i and the message using \(i). This way we know where each caramel chocolate is in the box.

Summary

Isn’t that cool?

Swift “if case let” lets us check for special things without writing too much code or using switch statements.

It also lets us give new names to things so we can use them later.

(Note: Portions of this blog were AI-assisted.)

--

--

Mark Moeykens

Mark creates tutorials at youtube.com/markmoeykens. He also has courses, books, and articles on this website: bigmountainstudio.com.