Esta página se muestra en inglés. Una traducción al español está en curso.
ConfigsJul 6, 2026·3 min de lectura

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.

Listo para agents

Instalación lista para agent

Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.

Native · 98/100Política: permitir
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Established
Entrada
SwiftGen Overview
Comando de instalación directa
npx -y tokrepo@latest install d7246edd-7959-11f1-9bc6-00163e2b0d79 --target codex

Ejecutar después de confirmar el plan con dry-run.

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

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados