---
title: "#52: R.swift 🎨"
url: https://littlebitesofcocoa.com/52-r-swift
author: Jake Marsh
published: 2015-08-04
topics: ["Libraries","Tools"]
description: "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..."
---

# #52: R.swift 🎨

By Jake Marsh, August 4, 2015. HTML: https://littlebitesofcocoa.com/52-r-swift

**[R.swift](http://git.io/rswift)** is a tool by [Mathijs Kadijk](https://github.com/mac-cain13) 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:

``` swift
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:

``` swift
let view = R.nib.controlPanelView.instantiateWithOwner(nil, options: nil)
```

Instantiate view controllers from Storyboards with ease:

``` swift
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:

``` swift
tableView.dequeueReusableCellWithIdentifier(
  R.reuseIdentifier.shipTableViewCell, 
  forIndexPath: indexPath
)
```

More info about **R.swift** can be found at [git.io/rswift](http://git.io/rswift)
