#52: R.swift 🎨


R.swift is a tool by Mathijs Kadijk that takes the resources in your app (images, segues, storyboards, nibs, etc.), examines them and generates a file in the root of your project called R.generated.swift containing a simple struct that maps your resources' actual names to Swift properties. R.swift updates the file each time you build, so you can focus on using your resources instead of managing them.

Why do this? For starters, it gives us Xcode native autocompleteion of resources names (finally). We're also saved from hardcoding strings throughout our app, or needing to go update a set of constants somewhere everytime we add a resource.

Let's look at some examples of where R.swift comes in handy. First up, images:

if spaceship.model == SpaceshipModel.Xwing {
  cell?.imageView!.image = R.image.shipXwing
} else {
  cell?.imageView!.image = R.image.shipGeneric

Nibs are also much easier to work with, here we instantiate the top view in a nib:

let view = R.nib.controlPanelView.instantiateWithOwner(nil, options: nil)

Instantiate view controllers from Storyboards with ease:

let spaceshipVC = R.storyboard.main.spaceshipViewController

R.swift also makes it easy to work with our cell reuse identifiers. It even includes extensions for UITableView and UICollectionView that save you from casting your cell's type:

  forIndexPath: indexPath

More info about R.swift can be found at