SwiftGen is a tool to auto-generate Swift code for resources of your projects, to make them type-safe. You can organize and manage your resources by using auto-generated swift code.

You can use SwiftGen for assets, colors, core data, fonts, interface builder files, JSON & YAML files, plists and localizable strings.


There are many ways of installing SwiftGen with project. I’ll use CocoaPods.

  1. Open terminal, navigate to your project and run pod init.
  2. Add pod ‘SwiftGen’ to your pod file.
  3. Run pod install.
  4. To invoke SwiftGen, go to Build Phases -> Add New Run Script Phase and add


    Everytime we build, SwiftGen is going to run and update all resources.

Configuration file

  • Create swiftgen.yml configuration file.

  • You can write inputs and outputs for the resources for which you want to generate swift code. Each action (xcassets, colors, fonts) in configuration file will generate constants for that type of input file. Inside output option templateName, define the Stencil template to use See more info here to generate the output.

    Let’s see example for images, colors and fonts. Add the below lines of code in swiftgen.yml file.

              inputs: Path/Assets.xcassets
                  - templateName: swift4
                    output: Path/SwiftGen/assets.swift
              inputs: Path/CustomAppColors.txt
                  - templateName: swift4
                    output: Path/SwiftGen/colors.swift
              inputs: Path/Fonts
                  - templateName: swift4
                    output: Path/SwiftGen/fonts.swift


  1. Add some resources to input files so we can test it.


    Add some Images to Assets.xcassets. For eg: I’ve added image named with header.png.


    I’ve created CustomAppColors.txt file and added hex color

     lightPurple         : #EBEEFF


    Create fonts folder and add font files .ttf/.otf. For eg: I’ve added font

  2. Create new group and name it SwiftGen. The generated output files will go inside this folder.

  3. After building project, Click SwiftGen Folder -> Show in Finder, drag and drop auto-genetrated assets.swift, colors.swift and fonts.swift files.

  4. You can use generated code for images, colors and fonts like

         let myImage = Asset.header.image
         let myImage =  UIImage(named: Asset.header.name)
         let myColor = ColorName.lightPurple.color
         let myColor = UIColor(named: .lightPurple)
         let myFont = FontFamily.Roboto.regular.font(size: 20.0)
         let myFont = UIFont(font: FontFamily.Roboto.regular, size: 20.0)


  • Avoid typos thanks to type safety.

  • Free auto-completion.

  • Avoid the risk of using an non-existing asset name.

  • All this will be ensured by the compiler.




Jigna Patel

iOS Developer

Always curious about learning new things

You may also like


In WWDC20, Apple introduced App Widgets to the home screen for multiple platforms(iOS, iPadOS, macOS). With WidgetKit, you can see the most important information from your app by putting widgets on your Home screen or Notification Center.

Engineering Awesome Conference 2020

We love engaging with the tech community, and we are big consumers of the awesome work that people share online. So a couple of weeks ago we hosted our first-ever online conference as a way to give back to the community. 6 experts from across our offices in Europe and...