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
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.
Failing gracefully, is what we want to achieve. Swift provides first-class support for handling error at runtime.
Here’s the trick❗️
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;
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? 🙃
Feedback,👏 and stay tuned for more swifty tricks.
Say hello on twitter @khfarooq.
Thanks for reading! 🌟