Skip to content

dbart01/Rigid

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 

Rigid

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:

if let image = UIImage(named: "Kittens") {
    // Use image here
}

with safe, compile-time-checked constants:

let image = UIImage(named: .Kittens)

This is all possible thanks to the enums generated by Rigid. The Rigid.swift file will contain something like this:

public enum Image: String {
    case Kittens  = "Kittens"
    case Puppies  = "Puppies"
    case Chickens = "Chickens"
    case Dolphins = "Dolphins"
    // etc...
}

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:

extension UIImage {
    convenience init(named name: Image) {
        self.init(named: name.rawValue)!
    }
}

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 of UIImage?
  • 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 click Build > Pre-actions
  • Click + to add New 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.
$(:Path to Rigid executable ->)"$PROJECT_DIR/$PROJECT/Rigid" $(:Path to Xcode project file ->)"$PROJECT_FILE_PATH" $(:Path to the generated .swift file ->)"$PROJECT_DIR/$PROJECT"
  • 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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages