Supercharge Swift with SwiftGen

The Swift code generation tool for colors, images, strings & more!

Gary Tokman
3 min readMar 3, 2021

We all know it’s a hassle to manually type out image and color names after adding them to your asset catalog.

let image = Image("my-cool-image") // Stop doing this 😡

If only there is a way to generate type-safe code that can reference the assets. Well, there is. Use a library called SwiftGen, and it will do all the heavy lifting for you. All that’s needed is to add it as a run script and configure it with a swiftgen.yml.

// swiftgen.ymlxcassets:
inputs:
- YourProjectName/Assets.xcassets // 1
outputs:
templateName: swift5 // 2
output: ProjectName/Assets.swift // 3

I mention this below but it’s best to add a pre-build run script in Xcode, so you don’t have to run swiftgen every time you update the assets with images/colors.

  1. The asset catalog file. You can also add colors to the asset catalog with the images.
  2. The template you are using, run swiftgen template list to try them out.
  3. The output file name for the generated code.

⚠️ SwiftGen supports color/image literals with the template literals-swift5 .

After doing so, run swiftgen if you did add the run-script from the project root, you will generate Swift code that you can easily access within your views.

generated swift code, ty swiftgen!
let image = Image(Asset.myCoolImage) // Do this 😎

Again, this is code that you don’t have to write or even check into git. It would be generated every time you build your Xcode project (pre-build run script. swiftgen). SwiftGen comes with an array of templates, and in addition to generating code for colors and images, it can also generate strings from a file. There are many different file types supported, and you can check them out in the docs here.

Do you want type-safe and localizable strings? Well, SwiftGen has you covered again. We’ll need to modify the swiftgen.yml and add String support.

// swiftgen.ymlstrings:
inputs: TemplateProject/Localizable.strings // 1
outputs:
- templateName: structured-swift5
output: TemplateProject/Generated/Strings.swift
  1. Input file where you can put all your strings, it’s important to namespace them here like this (the rest is the same as above):

// Localizable.strings file
"Onboarding.Buttons.Skip" = "Skip";
"Onboarding.Buttons.Join" = "Join";

Now we can access Strings in a type-safe way rather than scattering them around our views.

var body: some View {
Text(L10n.Onboarding.Buttons.join) // 🎉
}

⚠️ You can also concatenate Strings if they are dynamic using %@ in the Localizable.strings file.

The last thing I want to show about SwiftGen is that you can create your own code generation templates to achieve various tedious tasks. Here’s an example of that, this template generates colors and images compatible with SwiftUI (credit to sebj on GH for the template idea).

xcassets:
inputs:
- YourProjectName/Assets.xcassets
outputs:
templatePath: xcassets-swiftui.stencil // 1
output: ProjectName/Assets.swift
  1. Update the swiftgen.yml templateName field to templatePath above and point to the file after adding it to the project root (GH link above).

With the template above, after rebuilding, I can access the underlying asset and, because of an extension in the template file, now access the SwiftUI Image or Color .

var body: some View {
Asset
.myCoolImage
.image
.resizable()
.cornerRadius(10) // this compiles
}

And you can take this as far as your heart desires. I’ve seen custom templates that generate code when a type conforms to a protocol! I hope I convinced you to stop referencing your assets with strings and do it in a type-safe way with SwiftGen.

🔊 audio narration

Shameless plug: if you enjoyed this check my Patreon or YouTube for tutorials and posts I make around Swift and swe’ing in general 😎

--

--

No responses yet