This article accompanies the public SwiftGenLab GitHub repository and documents its production-minded SwiftGen setup. The goal is practical: keep native platform strengths, add SwiftGen where it improves safety, and wire the full flow so builds stay deterministic.

SwiftGen turns string-based resource lookups into typed Swift APIs for assets, localized strings, fonts, storyboards, and more. It moves many resource mistakes from runtime into compiler-checked symbols.

01

Why this SwiftGen reference exists

Modern Xcode already generates typed resource APIs for assets and string catalogs. SwiftGen is still valuable, but it is not the default answer for every resource.

  • Where native APIs are enough, keep native.
  • Where generated APIs materially reduce risk—especially localization keys and arguments—add SwiftGen.

The companion app demonstrates both approaches side-by-side in real code.

02

Machine and tool baseline

Observed local baseline

  • Xcode: 27.1 (27A9269)
  • Swift: 6.4
  • SwiftGen: 6.6.2

Commands used

Toolchain checks
cd /Users/mauro/files/code/uimatters/SwiftGenLab
xcodebuild -version
swift --version
swiftgen --version

Generator output can change across SwiftGen versions, build scripts depend on Xcode settings and sandboxing, and CI needs to match or pin expected versions to avoid drift.

Installation and team alignment

The current Run Script phase resolves swiftgen from $PATH, then checks common Homebrew locations. The binary is not currently vendored in the project.

OptionScopeReproducibility
ZIP vendored in repositoryProject-localHigh; version committed
CocoaPodsProject-local toolingHigh; version pinned
HomebrewMachine-wideMedium; depends on machine state
MintManaged CLIMedium-high when pinned
Build from sourceCustomVariable

Align the team by choosing one installation strategy, pinning the version, keeping one canonical config per module scope, linting before generation, and failing CI on generated-file drift.

Team validation
swiftgen --version
swiftgen config lint --config swiftgen.yml
swiftgen config run --config swiftgen.yml
git diff --exit-code
03

Native Xcode generation settings

SwiftGenLab keeps native Xcode generation features enabled alongside SwiftGen. Native APIs complement the generator rather than being replaced blindly.

AreaFormat / Xcode settingNative behaviorSwiftGen role
Icons, images, colors.xcassets; ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS=YESTyped symbols generated at build timeOptional typed API customization via the asset catalog parser
String Catalog.xcstrings; catalog generation enabledNative catalog workflow and generated symbolsUsually keep native-first
Legacy localization tables.strings and .stringsdictRuntime localization; call sites can remain stringly typedStrong fit for typed keys and format arguments
Script safetyENABLE_USER_SCRIPT_SANDBOXING=YESBuild scripts declare concrete inputs and outputsRequired for robust Run Script integration

Native asset symbols, String Catalog generation, and script sandboxing remain enabled in the project.

04

Repository layout used for SwiftGen

The layout keeps ownership explicit:

  • Root config: swiftgen.yml
  • Resources: SwiftGenLab/Assets.xcassets, Colors.xcassets, and Icons.xcassets
  • Localizations: SwiftGenLab/en.lproj and es.lproj
  • Template: SwiftGenLab/Templates/strings-structured-swift5-lab.stencil
  • Generated output: SwiftGenLab/Generated/SwiftGen/

Generated files include:

  • Assets+Generated.swift
  • L10n+Generated.swift
  • StringsLab+Generated.swift
05

SwiftGen configuration

The project root swiftgen.yml is the canonical configuration:

swiftgen.yml
xcassets:
  inputs:
    - SwiftGenLab/Assets.xcassets
    - SwiftGenLab/Colors.xcassets
    - SwiftGenLab/Icons.xcassets
  outputs:
    - templateName: swift5
      output: SwiftGenLab/Generated/SwiftGen/Assets+Generated.swift

strings:
  inputs:
    - SwiftGenLab/en.lproj
  outputs:
    - templateName: structured-swift5
      output: SwiftGenLab/Generated/SwiftGen/L10n+Generated.swift
    - templatePath: SwiftGenLab/Templates/strings-structured-swift5-lab.stencil
      output: SwiftGenLab/Generated/SwiftGen/StringsLab+Generated.swift
      params:
        enumName: StringsLab
        publicAccess: true
  • Multiple asset catalogs merge into one typed API surface.
  • One locale input avoids duplicate-table parser collisions.
  • Two string outputs provide the built-in L10n API and the custom StringsLab API.
06

Why .strings/.stringsdict instead of .xcstrings

The app intentionally uses Localizable.strings and Localizable.stringsdict in both locale directories. SwiftGen's strings parser provides typed keys and format arguments, while native String Catalog support remains enabled. Missing translations remain a runtime and data-quality concern and still need tests.

A deterministic migration path

  1. Keep legacy tables as the generation source while introducing a String Catalog in parallel.
  2. Freeze key naming rules and migrate table-by-table to control API churn.
  3. Diff generated files in CI and fail on unexpected symbol drift.
  4. Validate runtime parity with locale tests before switching call sites.
  5. Switch feature-by-feature, removing legacy tables only after parity is proven.

As catalog adoption grows, native Xcode extraction, editor support, translations, and generated symbols may reduce the overhead of custom templates and run scripts. A practical policy is to use SwiftGen for safer legacy call sites, migrate gradually, then re-evaluate whether it is still needed.

07

Build phase integration in Xcode

SwiftGen runs in a dedicated Xcode Run Script phase before source compilation:

Xcode Run Script
set -euo pipefail
export PATH="/opt/homebrew/bin:/usr/local/bin:$PATH"

if command -v swiftgen >/dev/null 2>&1; then
  SWIFTGEN_BIN="$(command -v swiftgen)"
elif [ -x /opt/homebrew/bin/swiftgen ]; then
  SWIFTGEN_BIN="/opt/homebrew/bin/swiftgen"
elif [ -x /usr/local/bin/swiftgen ]; then
  SWIFTGEN_BIN="/usr/local/bin/swiftgen"
else
  echo "error: SwiftGen is not installed or not in PATH."
  echo "error: Checked PATH and common locations: /opt/homebrew/bin/swiftgen, /usr/local/bin/swiftgen"
  exit 1
fi

cd "$SRCROOT"
"$SWIFTGEN_BIN" config lint --config "$SRCROOT/swiftgen.yml"
"$SWIFTGEN_BIN" config run --config "$SRCROOT/swiftgen.yml"

Why the script is production-ready

  • It fails fast when SwiftGen is missing.
  • It lints configuration before generation.
  • It resolves the executable from PATH and common install locations.
  • It uses the root configuration as the single source of truth.

Script sandboxing requirement

With ENABLE_USER_SCRIPT_SANDBOXING = YES, declare exact script inputs, including the config, catalogs, localization tables, and custom template, and outputs for all generated Swift files. This supports correct incremental builds.

08

Generated API usage in app code

The showcase view is SwiftGenLab/ContentView.swift.

Assets: SwiftGen and native APIs

Swift
Asset.Icons.caretLeftRenamed.swiftUIImage
Asset.Icons.caretRightLight.swiftUIImage
.background(Asset.Colors.statusWarning.swiftUIColor)

Image(.caretLeftRenamed)
Image(.caretRightLight)
.background(Color.statusWarning)

Localization: SwiftGen and native APIs

Swift
Text(L10n.Home.title)
Text(L10n.Home.welcome(username))
Text(L10n.Notifications.unread(unreadCount))
Text(L10n.Cart.items(unreadCount))

Text(
  String(
    format: NSLocalizedString(
      "home.welcome",
      tableName: "Localizable",
      bundle: .main,
      value: "Welcome, %@!",
      comment: ""
    ),
    username
  )
)

SwiftGen supplies compile-time signatures for formatted strings; native lookups remain available for a direct comparison.

09

Template customization and API visibility

The custom template strings-structured-swift5-lab.stencil is configured with enumName: StringsLab and publicAccess: true. It generates a public StringsLab enum, while the built-in output remains internal by default as L10n.

This makes generated-code visibility explicit and template-driven: internal APIs serve app-local usage, and a public variant is available when symbols need to cross module boundaries.

10

Module boundary and package integration

The app includes a local Swift package rooted in Packages/. Its InboxFeature product is linked by the app target, and its resources live under Packages/Sources/InboxFeature/Resources/.

Package generation is configured separately in Packages/swiftgen.package.yml and produces PackageL10n+Generated.swift. Package-generated strings resolve using Bundle.module through SwiftGen's BundleToken pattern under SWIFT_PACKAGE.

11

Resource and localization files

App localization resources

  • en.lproj/Localizable.strings and Localizable.stringsdict
  • es.lproj/Localizable.strings and Localizable.stringsdict

Package localization resources

  • Packages/Sources/InboxFeature/Resources/en.lproj/Localizable.strings
  • Packages/Sources/InboxFeature/Resources/es.lproj/Localizable.strings

App resource catalogs

  • SwiftGenLab/Assets.xcassets
  • SwiftGenLab/Colors.xcassets
  • SwiftGenLab/Icons.xcassets
12

Failure cases intentionally validated

The setup was hardened against common failure modes:

FailureExpected result / response
Wrong config input pathSwiftGen reports a file-not-found error.
Wrong template nameSwiftGen reports the missing template and guidance.
Duplicate localization table input across localesParser reports the duplicate; generate from one source locale.
SwiftGen missing from PATHThe build fails with an explicit script error.
Asset renamedNative stale references fail on the next build; SwiftGen references fail after regeneration.

These checks are essential for reliable team workflows.

13

Commands used day-to-day

Manual generation

Shell
cd /Users/mauro/files/code/uimatters/SwiftGenLab
swiftgen config lint --config swiftgen.yml
swiftgen config run --config swiftgen.yml

Build validation

Shell
cd /Users/mauro/files/code/uimatters/SwiftGenLab
xcodebuild -project SwiftGenLab.xcodeproj -scheme SwiftGenLab -configuration Debug -destination 'generic/platform=iOS Simulator' build

Package-level validation

Shell
cd /Users/mauro/files/code/uimatters/SwiftGenLab/Packages
swiftgen config lint --config swiftgen.package.yml
swiftgen config run --config swiftgen.package.yml
swift test
14

CI reference sequence

A minimal CI guard for deterministic generated code:

Shell
cd /path/to/SwiftGenLab
swiftgen --version
swiftgen config lint --config swiftgen.yml
swiftgen config run --config swiftgen.yml
git diff --exit-code
xcodebuild -project SwiftGenLab.xcodeproj -scheme SwiftGenLab -configuration Debug -destination 'generic/platform=iOS Simulator' build

An optional package guard can lint and run the package config, then run swift test.

15

Production decisions that survived

  1. Keep native Xcode generation enabled.
  2. Add SwiftGen where typed API value is clear.
  3. Use one canonical swiftgen.yml per module scope.
  4. Own custom templates only for narrow API-shape changes.
  5. Declare exact script inputs and outputs under sandboxing.
  6. Keep generated files in-repository for reviewability.
  7. Enforce generation consistency in CI.
  8. Validate bundle boundaries for package-owned resources.
16

Final architecture outcome

SwiftGenLab is a production-minded SwiftGen companion app with explicit generator configuration, deterministic build integration, typed assets and localization accessors, and native-versus-generated comparisons in real UI.

It also demonstrates custom template ownership with access control, local package integration, and generated package localization APIs—making it a practical reference for teams evaluating SwiftGen in modern Xcode projects.

Explore the complete source code in the SwiftGenLab GitHub repository.