Type-safe Resources With SwiftGen

There’s a great little library that will generate classes for accessing your resources, making them type-safe. It’s very simple to use and it will make your life a lot easier. In this article we’ll learn how to make type-safe resources with SwiftGen.

Installation

Like most of these libraries, there are many ways of installing SwiftGen. You can install it system-wide (homebrew, mint) or in your project directory (cocoapods). System-wide installation is easier to use and easier to keep up to date. If you install it in your project directory you can control exactly which version of the library you want running in which project. It’s up to you to decide which option works best for you. In this article we’ll go with the system-wide installation.

We’ll use homebrew to install the library:

Now that we have the library installed, we’ll add a new script run phase in our project. So that we can generate (and regenerate) our files every time we build the project:

Add this as the script:

You can find the original script with the instructions on the official SwiftGen documentation page.

One last thing left to do is to create a configuration file. In the root of your project directory create a file named ‘swiftgen.yml’. It doesn’t have to be a hidden file. Now we’re ready to generate some classes.

The way SwiftGen works is by using templates. You add the subcommands you want to use to your ‘swiftgen.yml’ file and specify the input, output and a template. You can see a list of templates in their GitHub repo. Let’s check out some of these subcommands.

Strings

If you want to use the strings subcommand you might update your .yml file like this:

Make sure you have some strings in your .strings file:

If you’re saving your generated files in a folder, just make sure the folder exists before building from Xcode. Now, if you build the project, you’ll have to manually add the generated file to your project. You’ll only have to do it once, of course πŸ™‚

Here’s how you would use it:

Since we were using the dot notation in our .strings file, SwiftGen created an enum to group our strings. And check out the usage of the parameterised string, how cool is that πŸ™‚

Assets

Let’s see how we can generate some constants out of our assets catalog:

Every Image in your assets catalog will now be accessible as a constant:

Now, if you accidentally rename your asset, the project won’t compile, how cool is that πŸ˜€

Interface Builder

This subcommand will create constants for your scenes and segues by using the storyboard identifiers.

Here’s a quick example how you would use it:

Let’s check one more useful subcommand…

Colors

This is a cool one. This subcommand will parse a file (text, json, xml or clr) extract colors from it and crate constants from them.

In the root of the project we have this file:

And here’s how we can use it in code:

How cool is that πŸ™‚

Conclusion

These are just some of the subcommands SwiftGen supports, you can play with a couple more like core data and fonts. This tool is very simple to use and works pretty well. It has some very useful subcommands, like for the strings and colors. If you’re using SwiftLint, it will even integrate with that πŸ™‚

This is definitely a tool that’s worth looking into and that will save you tons of time. I hope you’ve learned something fun today and as usual…

Have a nice day πŸ™‚
~D;

More resources

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.