The best ways to make sure your Swift code never crashes again

Dec 24, 2019 · 4 min read
Image for post
Image for post
Icons come from Freepik sites

All programs have situations where they can create errors.

This is almost inevitable but it is normal because we cannot predict the behaviour of the user of our program.

Rather than preventing all errors from occurring, we have to learn how to deal with them.

You have to learn how to manage them so that the program doesn’t crash and the user leaves.

Often, we forget to manage errors because we tell ourselves that it won’t happen or that it can’t happen.

But you can never be sure, so you have to be rigorous with that.

Image for post
Image for post

Guard let vs If let

The optionals are the first source of error in Swift.

When you unwrap them to access their value, you never know what will be there in advance.

Unless you specify it yourself beforehand.

Here’s how to unwrap them safely with guard let:

Using guard let is used to create a variable that will be accessible in the scope of the function that contains that declaration.

It is also used to make the function stop if you don’t get the value you want.

If ever, in a function, you don’t want to stop the function if you don’t have the value you want, or if you do something different depending on whether it’s there or not, then you can use if let:

I recommend you try to use guard let as much as possible because your code will be cleaner.

It avoids having a lot of nested if let if you have several options to handle which would not be very readable.

Try … catch

Sometimes we also have to handle cases in which we will return a subclass of the Error object.

Functions that are likely to return errors are marked are defined like this:

If a function is declared like this, you must call it in a try … catch block:

In this code, if the function returns an error of the type ErrorSubclass then we execute the code in the catch block.

Here are the different patterns you can use in a catch:

You can create your own errors based on what your code does.

And it is even strongly recommended to have full control over all possible situations in your code.

Here is an example:

This code will create a new type of error which is EcommerceError.

Here’s how we can use it:

In this code, every information about the product and its buyer is checked and depending on the possible error a certain type of error is returned.

This is how we would use the function buyItems:

In this code a different error is displayed depending on what went wrong in the buyItems function and the type of error created is used to warn the user of what went wrong.


With this article, you have everything you need to manage your code errors as well as possible.

It will be extremely useful to avoid crashing the application you are creating and thus losing users.

Thanks for reading this article!

If you want to learn more about Swift, here’s a great way to do it:

Pro Swift with Paul Hudson from Hacking with Swift

Flawless iOS

🍏 Community around iOS development, mobile design, and…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store