Get strong typed, autocompleted resources like images, fonts and segues in Swift projects
It makes your code that uses resources:
- Fully typed, less casting and guessing what a method will return
- Compile time checked, no more incorrect strings that make your app crash at runtime
- Autocompleted, never have to guess that image name again
Currently you type:
let icon = UIImage(named: "settings-icon")
let font = UIFont(name: "San Francisco", size: 42)
let viewController = CustomViewController(nibName: "CustomView", bundle: nil)
With R.swift it becomes:
let icon = R.image.settingsIcon()
let font = R.font.sanFrancisco(size: 42)
let viewController = CustomViewController(nib: R.nib.customView)
Check out more examples of R.swift based code!
Autocompleted images:
Compiletime checked images:
This is only the beginning, check out more examples!
After installing R.swift into your project you can use the R
-struct to access resources. If the struct is outdated just build and R.swift will correct any missing/changed/added resources.
R.swift currently supports these types of resources:
Runtime validation with R.assertValid()
:
- If all images used in storyboards are available
- If all view controllers with storyboard identifiers can be loaded
- Why was R.swift created?
- Why should I choose R.swift over alternative X or Y?
- What are the requirements to run R.swift?
- How to use methods with a
Void
argument? - How to fix missing imports in the generated file?
- How does R.swift work?
- How to upgrade to 1.0?
CocoaPods is the recommended way of installation, as this avoids including any binary files into your project.
There is also a short video of this instruction.
- Add
pod 'R.swift'
to your Podfile and runpod install
- In XCode: Click on your project in the file list, choose your target under
TARGETS
, click theBuild Phases
tab and add aNew Run Script Phase
by clicking the little plus icon in the top left - Drag the new
Run Script
phase above theCompile Sources
phase and belowCheck Pods Manifest.lock
, expand it and paste the following script:"$PODS_ROOT/R.swift/rswift" "$SRCROOT"
- Build your project, in Finder you will now see a
R.generated.swift
in the$SRCROOT
-folder, drag theR.generated.swift
files into your project and uncheckCopy items if needed
Tip: Add the *.generated.swift
pattern to your .gitignore
file to prevent unnecessary conflicts.
- Add the R.swift.Library to your project
- Download a R.swift release, unzip it and put it into your source root directory OR install the binary via homebrew. For more detail check Homebrew
- In XCode: Click on your project in the file list, choose your target under
TARGETS
, click theBuild Phases
tab and add aNew Run Script Phase
by clicking the little plus icon in the top left - Drag the new
Run Script
phase above theCompile Sources
phase, expand it and paste the following script:"$SRCROOT/rswift" "$SRCROOT"
- Build your project, in Finder you will now see a
R.generated.swift
in the$SRCROOT
-folder, drag theR.generated.swift
files into your project and uncheckCopy items if needed
Tip: Add the *.generated.swift
pattern to your .gitignore
file to prevent unnecessary conflicts.
R.swift
's binary is also available via homebrew. Homebrew is a packagemanager for OSX. Install it via the following command:
$ brew install rswift
Be also sure to update brew via $ brew update
If your paths are correct, rswift should be available via the command line.
$ rswift -h
usage: rswift [-p|--xcodeproj] [-t|--target] [--bundleIdentifier]
[--buildProductsDir] [--developerDir] [--sourceRoot] [--sdkRoot]
[--productModuleName] [-h|--help]
Please post any issues, questions and compliments in the GitHub issue tracker and feel free to submit pull request with fixes and improvements. Keep in mind; a good pull request is small, forked from the master
-branch and well explained. It also should benefit most of the users.
R.swift and R.swift.Library are created by Mathijs Kadijk and released under a MIT License.