Article

10 Swift Extensions We Use at Livefront

Add your own spin to Swift

Josh Rondestvedt

April 13, 2021

Let’s be honest. Swift and Apple’s frameworks don’t have all the functionality we need to build the best software for Apple devices. Fortunately, Swift supports extensions so we can add the missing pieces we need to make our lives easier.

If you’re new to Swift, please refer to the documentation to learn more about Extensions before continuing.

In this article, I will focus on extensions that add additional functionality to existing types. Extensions can also add default implementation for Protocols, add constraints to protocol types, and more.

When creating your own extensions, I would recommended creating a handful of unit tests to test the implementation to ensure you are receiving the desired outcome. In an effort to keep the gists below relatively short, I did not include our unit tests.

You can find the Xcode Playground used in this article on my GitHub page.

Here are just 10 of the many extensions we use at Livefront.

1. UIView — Constraints

Adding constraints to a UIView

Instead of trying to remember to set translatesAutoresizingMaskIntoConstraints to false, add the view to the parent view, and set all the individual constraints, this helper method will perform all these actions for you. This method allows you to set the top, leading, trailing, and bottom constraints to the parent view. If you omit one of the constraint parameters, the constraint will have a value of zero pinning the view to the edge of the parent view. If you want to completely cover the parent view, omit all the constraint parameters.

2. Date — UTC Date

Creating a Date object from a String in the UTC timezone

A REST API will typically return a date string in the UTC time zone. The static method above allows you to convert the string into a Date object. If you have problems with this extension in your own project, ensure the dateFormat matches the format of the date string you are receiving.

3. String — Retrieve URLs

Retrieve valid URLs from a String

This helper method is very handy when you have multiple URLs in a given string. I would highly recommend writing a few unit tests to ensure this method retrieves the intended URLs for your specific JSON response.

4. UIStackView — Remove Views

Removing all subviews from a UIStackView

There are a few steps that need to be performed when removing views from a UIStackView such as removing it from the stack view itself, deactivating any constraints, and completely removing from the parent view. This helper method will take care of all these steps for you.

5. Bundle — App Version and Build number

Retrieve the app version and build number

This is one of those features that should included in Bundle. Instead of trying to remember the obscure dictionary key, these computed properties will assist with the retrieval of the app version and build number. Many apps include the version number in their settings menu.

6. Calendar — Prior Year

Get the prior year as an Integer

This one is pretty straightforward. The method will return the prior year as an Integer.

7. UIStackView — Convenience Init

Convenience Init to make creation easier

Remembering what properties to set on a UIStackView can be challenging. This convenience initializer includes the common properties as its parameters. The initializer also sets translatesAutoresizingMaskIntoConstraints to false on each of the views.

8. UIColor — Hex

Get the Hex value of a UIColor

This method will retrieve the hex value of a UIColor and return it as a String. This can be extremely useful if you want to save and persist a color value for a user. This way you only need to save the hex string instead of the three integer RGB values.

9. UIViewController — Dark Mode

Check if dark mode is enabled

UIColors such as .label, .systemBlue, etc will automatically adjust when the user switches between light and dark mode, but you may want to add additional functionality when the user switches the device’s appearance. This computed property will allow you to check which appearance is active so you can respond accordingly.

10. UICollectionView — Last IndexPath

Get the last indexPath of a collectionView

Lastly, here is a method added to UICollectionView that will return the last valid indexPath. This is another one of those features that feels like it should already exist in UIKit. While this could be achieved by taking the count of the items in a collectionView and subtracting one in the view controller, adding it through an extension is a little safer.

Summary

I would argue that it’s almost impossible to create a project without adding a single extension. Adding functionality through extensions makes Swift more powerful and allows you to create new features in a safe way. I encourage you to search the web for “Swift Extensions” and have fun with all the creative solutions our fellow developers have come up with.

Feel free to share your favorite extension(s) in a comment below. 😃

Resources

Protocol Extensions Swift Extensions Type Constrains with Extensions
  • Josh Rondestvedt

    Software Engineer

    Josh Rondestvedt