Get strong typed, autocompleted resources like images, fonts and segues in Swift projects
Why use this?
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 color = UIColor(named: "indicator highlight")
let viewController = CustomViewController(nibName: "CustomView", bundle: nil)
let string = String(format: NSLocalizedString("welcome.withName", comment: ""), locale: NSLocale.current, "Arthur Dent")
With R.swift it becomes:
let icon = R.image.settingsIcon()
let font = R.font.sanFrancisco(size: 42)
let color = R.color.indicatorHighlight()
let viewController = CustomViewController(nib: R.nib.customView)
let string = R.string.localizable.welcomeWithName("Arthur Dent")
This is only the beginning, check out more examples!
CocoaHeadsNL presentation
Mathijs Kadijk presented R.swift at the September 2016 CocoaHeadsNL meetup.
Talking about the ideas behind R.swift and demonstrating how to move from plain stringly-typed iOS code to statically typed code.
Features
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:
Build your project, now the R struct should be available in your code, use auto-complete to explore all static references
CocoaPods
Add pod 'R.swift' to your Podfile and run pod install
In Xcode: Click on your project in the file list, choose your target under TARGETS, click the Build Phases tab and add a New Run Script Phase by clicking the little plus icon in the top left
Drag the new Run Script phase above the Compile Sources phase and belowCheck Pods Manifest.lock, expand it and paste the following script:
Add $SRCROOT/R.generated.swift to the “Output Files” of the Build Phase
Uncheck “Based on dependency analysis” so that R.swift is run on each build
Build your project, in Finder you will now see a R.generated.swift in the $SRCROOT-folder, drag the R.generated.swift files into your project and uncheckCopy items if needed
Add mac-cain13/R.swift to your Mintfile and run mint bootstrap to install this package without linking it globally (recommended)
In Xcode: Click on your project in the file list, choose your target under TARGETS, click the Build Phases tab and add a New Run Script Phase by clicking the little plus icon in the top left
Drag the new Run Script phase above the Compile Sources phase, expand it and paste the following script:
if mint list | grep -q 'R.swift'; then
mint run R.swift@v7.0.1 rswift generate "$SRCROOT/R.generated.swift"
else
echo "error: R.swift not installed; run 'mint bootstrap' to install"
return -1
fi
Add $SRCROOT/R.generated.swift to the “Output Files” of the Build Phase
Uncheck “Based on dependency analysis” so that R.swift is run on each build
Build your project, in Finder you will now see a R.generated.swift in the $SRCROOT-folder, drag the R.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.
Homebrew
R.swift is also available through Homebrew. This makes it possible to install R.swift globally on your system. Install R.swift by running: brew install rswift. The Homebrew formula is maintained by @tomasharkema.
Download a R.swift release, unzip it and put it into your source root directory
In Xcode: Click on your project in the file list, choose your target under TARGETS, click the Build Phases tab and add a New Run Script Phase by clicking the little plus icon in the top left
Drag the new Run Script phase above the Compile Sources phase, expand it and paste the following script:
Add $SRCROOT/R.generated.swift to the “Output Files” of the Build Phase
Uncheck “Based on dependency analysis” so that R.swift is run on each build
Build your project, in Finder you will now see a R.generated.swift in the $SRCROOT-folder, drag the R.generated.swift files into your project and uncheckCopy items if needed
R.swift

Get strong typed, autocompleted resources like images, fonts and segues in Swift projects
Why use this?
It makes your code that uses resources:
Currently you type:
With R.swift it becomes:
Check out more examples or hear about how Fabric.app uses R.swift!
Demo
Autocompleted images:
Compiletime checked images:
This is only the beginning, check out more examples!
CocoaHeadsNL presentation
Mathijs Kadijk presented R.swift at the September 2016 CocoaHeadsNL meetup. Talking about the ideas behind R.swift and demonstrating how to move from plain stringly-typed iOS code to statically typed code.
Features
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.validate()
:Q&A
Void
argument?Installation
As of Rswift 7, Swift Package Manager is the recommended method of installation.
Demo video: Updating from R.swift 6 to Rswift 7 (Starting at 1:06, this describes the installation of Rswift 7).
Xcode project - SPM
github.com/mac-cain13/R.swift
RswiftLibrary
RswiftGenerateInternalResources
RswiftModifyXcodePackages
, clickRun
, clickAllow Command to Change Files
R
struct should be available in your code, use auto-complete to explore all static referencesScreenshot of the Build Phase can be found here
Package.swift based SPM project
R
struct should be available in your code, use auto-complete to explore all static referencesCocoaPods
pod 'R.swift'
to your Podfile and runpod install
TARGETS
, click theBuild Phases
tab and add aNew Run Script Phase
by clicking the little plus icon in the top leftRun Script
phase above theCompile Sources
phase and belowCheck Pods Manifest.lock
, expand it and paste the following script:$SRCROOT/R.generated.swift
to the “Output Files” of the Build PhaseR.generated.swift
in the$SRCROOT
-folder, drag theR.generated.swift
files into your project and uncheckCopy items if needed
Screenshot of the Build Phase can be found here
Tip: Add the
*.generated.swift
pattern to your.gitignore
file to prevent unnecessary conflicts.Mint
mac-cain13/R.swift
to your Mintfile and runmint bootstrap
to install this package without linking it globally (recommended)TARGETS
, click theBuild Phases
tab and add aNew Run Script Phase
by clicking the little plus icon in the top leftRun Script
phase above theCompile Sources
phase, expand it and paste the following script:$SRCROOT/R.generated.swift
to the “Output Files” of the Build PhaseR.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.Homebrew
R.swift is also available through Homebrew. This makes it possible to install R.swift globally on your system. Install R.swift by running:
brew install rswift
. The Homebrew formula is maintained by @tomasharkema.Manually
TARGETS
, click theBuild Phases
tab and add aNew Run Script Phase
by clicking the little plus icon in the top leftRun Script
phase above theCompile Sources
phase, expand it and paste the following script:$SRCROOT/R.generated.swift
to the “Output Files” of the Build PhaseR.generated.swift
in the$SRCROOT
-folder, drag theR.generated.swift
files into your project and uncheckCopy items if needed
Screenshot of the Build Phase can be found here
Tip: Add the
*.generated.swift
pattern to your.gitignore
file to prevent unnecessary conflicts.Contribute
We’ll love contributions, read the contribute docs for info on how to report issues, submit ideas and submit pull requests!
License
R.swift and R.swift.Library are created by Mathijs Kadijk and released under a MIT License.
Special thanks to Tom Lokhorst for his major contributions and help maintaining this project.