SwiftGen is a suite of tools written in Swift to auto-generate Swift code (or anything else actually) for various assets of your project:
enums
for your Assets Catalogs imagesenums
for yourLocalizable.strings
strings.enums
for your UIStoryboards and their Scenesenums
for your NSStoryboards and their Scenesenums
for your Colors.enums
for your Fonts.
There are multiple benefits in using this:
- Avoid any typo you could have when using a String
- Free auto-completion
- Avoid the risk to use an non-existing asset name
- All this will be ensured by the compiler.
Via CocoaPods
If you're using CocoaPods, you can simply add pod 'SwiftGen'
to your Podfile
.
This will download the SwiftGen
binaries and dependencies in Pods/
during your next pod install
execution
and will allow you to invoke it via $PODS_ROOT/SwiftGen/bin/swiftgen
in your Script Build Phases.
Compile from source
Alternatively, you can clone the repository and use rake install
to build the tool.
With this solution you're sure to build and install the latest version from master
.
You can install to the default locations (no parameter) or to custom locations:
# Binary is installed in `./swiftgen/bin`, frameworks in `./swiftgen/lib` and templates in `./swiftgen/templates`
$ rake install
# - OR -
# Binary will be installed in `~/swiftgen/bin`, framworks in `~/swiftgen/fmk` and templates in `~/swiftgen/tpl`
$ rake install[~/swiftgen/bin,~/swiftgen/fmk,~/swiftgen/tpl]
The tool is provided as a unique swiftgen
binary command-line, with the following subcommands:
swiftgen images [OPTIONS] DIR
swiftgen strings [OPTIONS] FILE
swiftgen storyboards [OPTIONS] DIR
swiftgen colors [OPTIONS] FILE
swiftgen fonts [OPTIONS] DIR
Each subcommand has its own option and syntax, but some options are common to all:
--output FILE
or-o FILE
: set the file where to write the generated code. If omitted, the generated code will be printed onstdout
.--template NAME
or-t NAME
: define the Stencil template to use (by name, see here for more info) to generate the output.--templatePath PATH
or-p PATH
: define the Stencil template to use, using a full path.
You can use --help
on swiftgen
or one of its subcommand to see the detailed usage.