SwiftGen /SwiftGen
The Swift code generator for your assets, storyboards, Localizable.strings, … — Get rid of all String-based APIs!
★ 9,543⑂ 761👁 100⑃ 490● 621▤ 44
+0
日增长
9,543
stars
761
forks
100
watchers
490
prs
621
issues
44
releases
README
SwiftGen
!Swift 4.x !Swift 5.x
SwiftGen is a tool to automatically generate Swift code for resources of your projects (like images, localised strings, etc), to make them type-safe to use.
Installation
Configuration File
Choosing your template
Additional documentation
Then generate constants for:
Assets Catalogs
Colors
Core Data
Files
Fonts
Interface Builder files
JSON and YAML files
Plists
Localizable strings
There are multiple benefits in using this:
Avoid any risk of typo when using a String
Free auto-completion
Avoid the risk of using a non-existing asset name
All this will be ensured by the compiler and thus avoid the risk of crashing at runtime.
Also, it's fully customizable thanks to Stencil templates, so even if it comes with predefined templates, you can make your own to generate whatever code fits your needs and your guidelines!
Installation
There are multiple possibilities to install SwiftGen on your machine or in your project, depending on your preferences and needs:
Download the ZIP for the latest release
Go to the GitHub page for the latest release
Download the swiftgen-x.y.z.zip file associated with that release
Extract the content of the zip archive in your project directory
We recommend that you unarchive the ZIP inside your project directory and commit its content to git. This way, all coworkers will use the same version of SwiftGen for this project.
If you unarchived the ZIP file in a folder e.g. called swiftgen at the root of your project directory, you can then invoke SwiftGen in your Script Build Phase using:
sh
"${PROJECT_DIR}/swiftgen/bin/swiftgen" …
---
Via CocoaPods
If you're using CocoaPods, simply add pod 'SwiftGen', '~ 6.0' to your Podfile.
Then execute pod install --repo-update (or pod update SwiftGen if you want to update an existing SwiftGen installation) to download and install the SwiftGen binaries and dependencies in Pods/SwiftGen/bin/swiftgen next to your project.
Given that you can specify an exact version for SwiftGen in your Podfile, this allows you to ensure all coworkers will use the same version of SwiftGen for this project.
You can then invoke SwiftGen in your Script Build Phase using:
sh
if [[ -f "${PODS_ROOT}/SwiftGen/bin/swiftgen" ]]; then
"${PODS_ROOT}/SwiftGen/bin/swiftgen" …
else
echo "warning: SwiftGen is not installed. Run 'pod install --repo-update' to install it."
fi
Similarly, be sure to use Pods/SwiftGen/bin/swiftgen instead of just swiftgen where we mention commands with swiftgen in the rest of the documentation.
_Note: SwiftGen isn't really a pod, as it's not a library your code will depend on at runtime; so the installation via CocoaPods is just a trick that installs the SwiftGen binaries in the Pods/ folder, but you won't see any swift files in the Pods/SwiftGen group in your Xcode's Pods.xcodeproj. That's normal; the SwiftTop issues
#1065Xcode 15 String catalog support 22 comments · 2023-06-08#1110SwiftGen Errors on Swift 621 comments · 2024-03-05#603Inconvenient access to Assets of type color & image20 comments · 2019-02-11#685SwiftUI use of LocalizedStringKey19 comments · 2020-03-26#69Add support for accessibility labels17 comments · 2015-12-03#19Support UITableViewCell & UICollectionViewCell identifiers17 comments · 2015-09-25#627Add templates for Lottie15 comments · 2019-03-19#750SwiftUI support13 comments · 2020-07-10
Discussions
#1164[email protected]0 comments · 2026-09-03#1145Plist runtime-swift5.stencil update for Swift 60 comments · 2025-03-24#1142Is SwiftGen dead?2 comments · 2024-11-14#1139Ordered search for assets0 comments · 2024-09-06#1130Stencil Custom Extension1 comments · 2024-06-30#1116No such module 'AppKit'1 comments · 2024-03-20#1094How can I change Locale.current?0 comments · 2023-12-22#1071XCode 15 Strings Catalog1 comments · 2023-07-26
Latest release
6.6.3 6.6.3 2024-03-02Breaking Changes _None_ New Features CLI: Added support for --quiet and --verbose flags to swiftgen run. John Szumski 1007 1008 Bug Fixes Build: Fixed the rake cli:install command and with it the Homebrew formula. Liquidsoul 1030 1104 1105 Internal Changes _None_