Find it on GitHub.
String-based operations are inherently unsafe, error-prone and lack basic convenience from code completion and compiler checks. Rigid fixes all of that. Inspired by the WWDC 2015 session – Swift In Practice (#411), Rigid is a command-line utility that quickly embeds into your Xcode project and provides generated .swift
code for all your static assets, .xib
files, segue identifier, view controller identifiers, and more.
How It Works
Rigid will automatically scan your Xcode project directory looking for images, nibs, storyboards, etc. It will then generate a .swift
file with typed constants and convenience methods for common operations like instantiating a UIImage
, UINib
, UIViewController
, etc. It is also cross-platform and will seamlessly work on both iOS and Mac OS X without any additional configuration!
It replaces using error-prone string literals:
with safe, compile-time-checked constants:
This is all possible thanks to the enums generated by Rigid. The Rigid.swift
file will contain something like this:
But that isn’t enough. We also need a way to use the Image
enum. Rigid also provides overrides for common use cases like instantiating an image from your bundle:
Why It Matters
While the example above may seem like a trivial change, we do, in fact, gain several very important advantages from using Rigid’s generated code:
- the initializer is guaranteed to succeed and will therefore return a
UIImage
instead ofUIImage?
- if the image is removed, renamed or somehow is no longer available, a compile-time error will be emitted instead of silently failing at runtime, when it matters
- code completion!
Installation & Using Rigid
Since Rigid isn’t actually a dependency, it will NOT contribute to dependency “bloat”. Adding Rigid is extremely simple and will not impose ANY limitations on your projects as everything is self contained.
- Clone / download this repository
- Build the project
- Building the project will automatically copy the Rigid executable to your desktop, locate it, drag it into your project directory (doesn’t have to be part of your Xcode project, just need to be in the same directory as your other source files)
- Go to
Product > Scheme > Edit Scheme
and in the left sidebar clickBuild > Pre-actions
- Click
+
to addNew Run Script Action
- Click the dropdown that says
Provide build settings from
and select your target - Copy / paste the following snippet to execute Rigid with every build and provide arguments. These defaults will work well for a standard project structure generated by Xcode. If your structure differs, be sure to provide the path to the Rigid executable and the destination path for the generated
.swift
file. - Build your target
- Navigate to your project in Finder and locate
Rigid.swift
and add it to your project - Now, every time your project is built, Rigid will update the
.swift
without any more input from you
Script Action
With comments
No comments