Validating data is usually one of the "chores" we encounter while building apps. It can be annoying and boring code to write, and can be a huge source of bugs. Today we'll check out a great library called FormValidatorSwift that can help us avoid all these issues. Let's take a look.
The core of FormValidatorSwift is built around checking if a String passes some sort of test to be deemed 'valid'.
We'll use individual Conditions or combined sets of Conditions called Validators. We'll attach these to controls like Text Fields and Text Views to allow them to be validated.
Let's try out the included ValidatorTextField to validate an email address:
First up, Equatables. We can use XcodeEquatableGenerator from Serg Dort to quickly generate the long list of property comparisons often required when adopting the Equatable protocol in our Swift Structs or Classes:
Last but definitely not least, is XcodeWay by Khoa Pham. This one is a bit different in that it doesn't modify or generate any code, but rather lets us quickly open a bunch of commonly used directories, by adding them to our Editor Menu:
Nice! No more hunting around in Finder or Terminal for these.
Today we're going to continue our look at the new Measurement functionality in Foundation. (Bite #276). MeasurementFormatter offers some great ways to format and print Strings of our Measurements. Let's take a look.
First we'll make define some units to work with. One of the most common ones we'll use in our apps is Length (for example when displaying how "far away" something is).
Last but certainly not least, we can nicely format a measurement into a String. Apple provides a MeasurementFormatter type that is fully locale-aware. This allows us to easily display Measurements in our apps in whichever ways our users expect.
Xcode Source Editor Extensions are really starting to come into their own. Today we'll look at one that solves an age-old annoyance for Xcode users: Importing.
We've all been there. We're deep down in the depths of file, and we realize we need to import a module. We dutifully scroll all the way up, type the import, then scroll back down trying to find our place, and get back in "the zone". Yuck.
That's it, Rollout is now integrated into our app. Neat!
Now let's fast-forward a bit, to a time after we've shipped our imaginary app to the App Store. We get approved, and we're celebrating the launch and then 😣 doh! Something is causing our app to crash on launch. Yikes.
First we'll take a look at our project, and find the culprit. In our case, it looks like part of our code is throwing a non-fatal exception when we didn't expect it to. Yuck.
Not to worry though, because our app has Rollout inside.
We'll head to the Rollout dashboard and add a hotfix.
We'll select the "Surround Invocation with Try Catch" type, and test it out.
That fixes it! Now that things are fixed up, we can switch our hot patch to "Production" mode. (We could even release it gradually to only a percentage of our user base).
This kind of flexibility and freedom really does feel like magic. It's sort of like "insurance" knowing we won't be stuck waiting on Apple to get critical changes out to users. Neat!
Rollout supports Objective-C apps, Swift is supported (in beta) and will be fully released in November.
Last but certainly not least, Rollout is offering a sweet discount for LBOC readers: Use the code cocoa20 to get 20% off any paid plan for 6 months!
A huge thanks to Rollout for sponsoring this week's Bites!
Attributed Strings are a fantastic way to work with rich, styled-text. (We've covered them in Bites #143 & #144 for the curious.) We've even looked at ways to improve upon how verbose the FoundationNSAttributedString API can be.
Today we'll check out another approach using a library from Eddie Kaiger called SwiftyAttributes. It dramatically improves how we work with Attributed Strings. Let's dive in.
Let's start by looking at how things work in vanilla Foundation:
Things get even nicer when we need to concatenate two NSAttributedStrings together. We've all had to do this before and it could have involved applying attributes to specific ranges, etc. Now, we can use a much more declarative and readable syntax:
letsomeString="We can easily ".withFont(.systemFont(ofSize:16))+"underline".withFont(.systemFont(ofSize:16)).withUnderlineStyle(.styleSingle)+" certain words.".withFont(.systemFont(ofSize:16))
Under the hood, SwiftyAttributes takes a pragmatic approach by extending both Swift's String type as well as NSAttributedString with a fairly comprehensive set of delicious, sugary functions: