# SwiftGen — Code Generator for Swift Project Resources > A template-driven tool that generates Swift code for assets, storyboards, strings, colors, and fonts to eliminate stringly-typed resource access. ## Install Save in your project root: # SwiftGen — Code Generator for Swift Project Resources ## Quick Use ```bash brew install swiftgen # Create swiftgen.yml in your project root: # strings: # inputs: Resources/en.lproj/Localizable.strings # outputs: # templateName: structured-swift5 # output: Generated/Strings.swift # xcassets: # inputs: Resources/Assets.xcassets # outputs: # templateName: swift5 # output: Generated/Assets.swift swiftgen ``` ## Introduction SwiftGen scans your project resources and generates Swift enums and structs that provide type-safe access to images, colors, localized strings, fonts, storyboards, and more. By replacing string-based references with generated constants, it catches missing or renamed resources at compile time. ## What SwiftGen Does - Parses asset catalogs (xcassets), storyboards, XIBs, string tables, color palettes, font files, and plists - Generates Swift source files using customizable Stencil templates - Provides structured enum-based access with namespacing that mirrors your resource folder structure - Supports multiple output formats including flat constants and nested hierarchies - Integrates into Xcode build phases or runs as a standalone CLI command ## Architecture Overview SwiftGen is a command-line tool written in Swift. It uses dedicated parsers for each resource type (Core Data models, plists, IB files, asset catalogs, string tables). Parsed resource metadata is passed to Stencil templates that render the final Swift code. The template engine is fully configurable, so teams can customize output format, naming conventions, and import statements. ## Self-Hosting & Configuration - Install via Homebrew: `brew install swiftgen` - Or via CocoaPods, Mint, or SPM build plugin - Configure resource inputs and output paths in a swiftgen.yml file - Add a Run Script Build Phase in Xcode to regenerate on each build - Custom templates can be placed in your project and referenced by path ## Key Features - Fully customizable output through Stencil templates - Supports all major resource types: images, colors, strings, fonts, storyboards, nibs, Core Data models, plists, JSON, YAML - Namespace-aware generation that mirrors your asset catalog folder structure - Built-in templates for Swift 5 with both flat and structured output styles - Integrates with CI pipelines for validating resource consistency ## Comparison with Similar Tools - **R.swift** — similar goal but uses a fixed output format; SwiftGen offers template customization - **Xcode asset catalog compiler** — handles images and colors only; no strings, fonts, or storyboards - **String literal references** — zero tooling cost but fragile and not autocompleted - **Sourcery** — general-purpose Swift code generation; not specialized for resources - **Tuist** — project generation tool with resource accessors; requires adopting Tuist as build system ## FAQ **Q: Can I use SwiftGen and R.swift together?** A: Technically yes, but they serve the same purpose. Pick one to avoid duplicate generated code. **Q: Does SwiftGen support localized strings with format arguments?** A: Yes. It generates functions with typed parameters for strings containing %@ or %d placeholders. **Q: How do I customize the generated output?** A: Write or modify Stencil templates and reference them in your swiftgen.yml with templatePath instead of templateName. **Q: Does SwiftGen work with Swift Package Manager projects?** A: Yes. It can be used as a CLI tool or as an SPM build plugin to run automatically during builds. ## Sources - https://github.com/SwiftGen/SwiftGen - https://github.com/SwiftGen/SwiftGen/blob/stable/Documentation/Articles/Xcode-Integration.md --- Source: https://tokrepo.com/en/workflows/asset-d7246edd Author: AI Open Source