AutoAssets build script

22 Jul

Working with Xcode image assets in code is pretty easy. Just include the images in a Xcode assets file and throw in some UIImage calls like so:

UIImage(named:"someImage")

Although it’s unlikely to cause any crashers, you can easily end up having a typo somewhere and ship your app with a missing image. Not to mention you have to remember and type out the image name every time you want to use it.

There are better ways of course. For example, you can make it a bit better by using string constants. You’ll end up with something like this:

UIImage(named: LAMSomeImage)

Or you could wrap them in a category

LAMAssets.SomeImage()

But Jesse Squires proposes a better way using enums, which also provides proper name spacing. You should really read his post, it has some great stuff on dealing with colors too.

With Jesse’s method you access the images using the following style

Assets.SomeImage.image()

The difference between his enum style and using a category is subtle in use, but significant in implementation.

Inspired by his post, but not wanting to keep enums up-to-date with my Xcode Assets, I made AutoAssets.

AutoAssets (written in Swift) is a build script that generates a Swift source file at build time based on the projects assets. Basically it takes Jesse’s enum idea and automates it.

Here’s the repo