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.
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.
Machine and tool baseline
Observed local baseline
- Xcode:
27.1 (27A9269) - Swift:
6.4 - SwiftGen:
6.6.2
Commands used
cd /Users/mauro/files/code/uimatters/SwiftGenLab
xcodebuild -version
swift --version
swiftgen --versionGenerator 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.
| Option | Scope | Reproducibility |
|---|---|---|
| ZIP vendored in repository | Project-local | High; version committed |
| CocoaPods | Project-local tooling | High; version pinned |
| Homebrew | Machine-wide | Medium; depends on machine state |
| Mint | Managed CLI | Medium-high when pinned |
| Build from source | Custom | Variable |
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.
swiftgen --version
swiftgen config lint --config swiftgen.yml
swiftgen config run --config swiftgen.yml
git diff --exit-codeNative Xcode generation settings
SwiftGenLab keeps native Xcode generation features enabled alongside SwiftGen. Native APIs complement the generator rather than being replaced blindly.
| Area | Format / Xcode setting | Native behavior | SwiftGen role |
|---|---|---|---|
| Icons, images, colors | .xcassets; ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS=YES | Typed symbols generated at build time | Optional typed API customization via the asset catalog parser |
| String Catalog | .xcstrings; catalog generation enabled | Native catalog workflow and generated symbols | Usually keep native-first |
| Legacy localization tables | .strings and .stringsdict | Runtime localization; call sites can remain stringly typed | Strong fit for typed keys and format arguments |
| Script safety | ENABLE_USER_SCRIPT_SANDBOXING=YES | Build scripts declare concrete inputs and outputs | Required for robust Run Script integration |
Native asset symbols, String Catalog generation, and script sandboxing remain enabled in the project.
Repository layout used for SwiftGen
The layout keeps ownership explicit:
- Root config:
swiftgen.yml - Resources:
SwiftGenLab/Assets.xcassets,Colors.xcassets, andIcons.xcassets - Localizations:
SwiftGenLab/en.lprojandes.lproj - Template:
SwiftGenLab/Templates/strings-structured-swift5-lab.stencil - Generated output:
SwiftGenLab/Generated/SwiftGen/
Generated files include:
Assets+Generated.swiftL10n+Generated.swiftStringsLab+Generated.swift
SwiftGen configuration
The project root swiftgen.yml is the canonical configuration:
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
L10nAPI and the customStringsLabAPI.
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
- Keep legacy tables as the generation source while introducing a String Catalog in parallel.
- Freeze key naming rules and migrate table-by-table to control API churn.
- Diff generated files in CI and fail on unexpected symbol drift.
- Validate runtime parity with locale tests before switching call sites.
- 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.
Build phase integration in Xcode
SwiftGen runs in a dedicated Xcode Run Script phase before source compilation:
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.
Generated API usage in app code
The showcase view is SwiftGenLab/ContentView.swift.
Assets: SwiftGen and native APIs
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
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.
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.
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.
Resource and localization files
App localization resources
en.lproj/Localizable.stringsandLocalizable.stringsdictes.lproj/Localizable.stringsandLocalizable.stringsdict
Package localization resources
Packages/Sources/InboxFeature/Resources/en.lproj/Localizable.stringsPackages/Sources/InboxFeature/Resources/es.lproj/Localizable.strings
App resource catalogs
SwiftGenLab/Assets.xcassetsSwiftGenLab/Colors.xcassetsSwiftGenLab/Icons.xcassets
Failure cases intentionally validated
The setup was hardened against common failure modes:
| Failure | Expected result / response |
|---|---|
| Wrong config input path | SwiftGen reports a file-not-found error. |
| Wrong template name | SwiftGen reports the missing template and guidance. |
| Duplicate localization table input across locales | Parser reports the duplicate; generate from one source locale. |
| SwiftGen missing from PATH | The build fails with an explicit script error. |
| Asset renamed | Native stale references fail on the next build; SwiftGen references fail after regeneration. |
These checks are essential for reliable team workflows.
Commands used day-to-day
Manual generation
cd /Users/mauro/files/code/uimatters/SwiftGenLab
swiftgen config lint --config swiftgen.yml
swiftgen config run --config swiftgen.ymlBuild validation
cd /Users/mauro/files/code/uimatters/SwiftGenLab
xcodebuild -project SwiftGenLab.xcodeproj -scheme SwiftGenLab -configuration Debug -destination 'generic/platform=iOS Simulator' buildPackage-level validation
cd /Users/mauro/files/code/uimatters/SwiftGenLab/Packages
swiftgen config lint --config swiftgen.package.yml
swiftgen config run --config swiftgen.package.yml
swift testCI reference sequence
A minimal CI guard for deterministic generated code:
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' buildAn optional package guard can lint and run the package config, then run swift test.
Production decisions that survived
- Keep native Xcode generation enabled.
- Add SwiftGen where typed API value is clear.
- Use one canonical
swiftgen.ymlper module scope. - Own custom templates only for narrow API-shape changes.
- Declare exact script inputs and outputs under sandboxing.
- Keep generated files in-repository for reviewability.
- Enforce generation consistency in CI.
- Validate bundle boundaries for package-owned resources.
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.