SwiftGen

Swift’s enums are pretty handy. I love replacing any kind of string value that I would otherwise have to type with an enum, saves me time because Xcode autocompletes it, and saves me from typos and maintenance, as I’ve centralised the value that way.

A classic example of string values are Storyboard and cell identifiers, and images names. I always found it annoying to have to type them, and since I have a short memory for that kind of things I always have to go back to look them up first.

Today’s bite sized tutorial is dedicated to SwiftGen by Oliver Halligon a tool that will generate enums with the values for Storyboard identifiers, images in the asset catalog, and much more.

Let’s see how it works.

How to generate and use Storyboard identifiers enums with SwiftGen

Running swiftgen storyboard ProjectFolder will print out all the generated enums and support code.

This code will look something like this:

struct StoryboardScene {
  enum LaunchScreen : StoryboardSceneType {
    static let storyboardName = "LaunchScreen"
  }
  enum Main : StoryboardSceneType {
    static let storyboardName = "Main"
  }
}

struct StoryboardSegue {
  enum Main : String, StoryboardSegueType {
    case Details = "Details"
    case SegueA = "SegueA"
    case SegueB = "SegueB"
  }
}

Plus type definitions for StoryboardScene and StoryboardSegue, as well as extensions to use them with UIViewController.

The first time you’ll put the code in your app you should run the command it like this:

swiftgen storyboard ProjectFolder | pbcopy

This way all the generated code will be put in your pasteboard, so that you can paste it into a dedicated new file for your Storyboard enums.

Once the file is there you can make SwiftGen update it like this using the --output option:

swiftgen storyboard ProjectFolder --output ProjectFolder/path/to/file.swift

How to generate and use images names enums with SwiftGen

If you want to generate enums for your images in your assets catalog instead this is the command you should use:

swiftgen images ProjectFolder

And the generated code would look like this:

extension UIImage {
  enum Asset : String {
    case Dog = "dog"
    case Cat = "cat"
    case Elf = "elf"

    var image: UIImage {
      return UIImage(asset: self)
    }
  }

  convenience init!(asset: Asset) {
    self.init(named: asset.rawValue)
  }
}

All the considerations for the Storyboard segues and scenes example are valid for this one too.

Next Steps

Alongside images and Storyboard identifiers SwiftGen currently provides methods to generate enums for colors and localizable strings.

Finally a nice feature of SwiftGen is the option of providing your own templates for the generated code, so that if you need to provide a certain header, or have unconventional coding conventions you can have SwiftGen use those to generate your enums code.

Head over to the project repo on GitHub to find out more about SwiftGen, or checkout the example project for this tutorial to see it in action.

The project’s issues pages has a list of all the desired and work in progress new features, you could filter by the “help wanted” label and see if you can help.


That’s it for today. See you tomorrow with a library to abstract the storage of your app’s preferences. Subscribe to the email list to avoid missing out.

If you found this post useful and want to support the Advent Calendar please consider sharing it on your favourite social network. Thanks 🎅