Alertable Errors in Swift ⛔️ by@khfarooq

Alertable Errors in Swift ⛔️

Read on Terminal Reader
react to story with heart
react to story with light
react to story with boat
react to story with money
image
Khawaja Farooq HackerNoon profile picture

Khawaja Farooq

image
img source: pexel.com
Error is of vital importance. It can occur while communicating with network, disk read or in a runtime situation. Let’s here it 🔉
‘A value used to report that an error condition occurred and normal functionality was skipped’
-Matt Gallagher, Cocoa with love

Motivation

Recently, I came across a snippet by Ritesh Gupta exhibiting some cool techniques about UIAlertController, explaining how to avoid the boilerplate code using swift extensions.

Dirty Trick 💩

Failing gracefully, is what we want to achieve. Swift provides first-class support for handling error at runtime.
Here’s the trick❗️
image

Real Life Usage 🌏

Let’s take an example of failing gracefully; So we have an app that tells your age based on your date of birth that you input. That’s lame 😑
We need to come up with some validation rules for DOB. We can cater all of them through our custom error;
image
So Here’s the deal; basically the idea is to shout whenever you came across an error
do {    let yourAge = try age(from: yearOfBirth)  } catch {     error.alert(with: self)  }
Cool isn’t it? 🙃

Author🙏

Feedback,👏 and stay tuned for more swifty tricks.
Say hello on twitter @khfarooq.
Thanks for reading! 🌟
react to story with heart
react to story with light
react to story with boat
react to story with money
L O A D I N G
. . . comments & more!