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")
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.
Q&A
- 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 to use classes with the same name as their module?
- Can I ignore resources?
- Can I use R.swift in a library?
- How does R.swift work?
- How to upgrade to a new major version?
- How can I only run specific generators?
Xcode project - SPM
- In Project Settings, on the tab “Package Dependencies”, click “+” and add
github.com/mac-cain13/R.swift
- Select your target, on the tab “General”, in the section “Frameworks, Libraries, and Embeded Content”, click “+” and add
RswiftLibrary
- Select your target, on the tab “Build Phases”, in the section “Run Build Tool Plug-ins”, click “+” and add
RswiftGenerateInternalResources
- Build your project, now the
R
struct should be available in your code, use auto-complete to explore all static references
Screenshot of the Build Phase can be found here
When running on Xcode Cloud
- Add a custom build script in
ci_scripts/ci_post_clone.sh
with the content:defaults write com.apple.dt.Xcode IDESkipPackagePluginFingerprintValidatation -bool YES
Package.swift based SPM project
- Add a dependency in Package.swift:
dependencies: [ .package(url: "https://github.com/mac-cain13/R.swift.git", from: "7.0.0") ]
- For each relevant target, add a dependency and a plugin
.target( name: "Example", dependencies: [.product(name: "RswiftLibrary", package: "R.swift")], plugins: [.plugin(name: "RswiftGeneratePublicResources", package: "R.swift")] )
- Build your project, now the
R
struct should be available in your code, use auto-complete to explore all static references
CocoaPods
1. Add `pod 'R.swift'` to your [Podfile](http://cocoapods.org/#get_started) and run `pod install`
2. 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
3. Drag the new `Run Script` phase **above** the `Compile Sources` phase and **below** `Check Pods Manifest.lock`, expand it and paste the following script:
```bash
"$PODS_ROOT/R.swift/rswift" generate "$SRCROOT/R.generated.swift"
```
4. Add `$SRCROOT/R.generated.swift` to the "Output Files" of the Build Phase
5. Uncheck "Based on dependency analysis" so that R.swift is run on each build
6. 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 **uncheck** `Copy items if needed`
_Screenshot of the Build Phase can be found [here](Documentation/Images/BuildPhaseExample.png)_
_Tip:_ Add the `*.generated.swift` pattern to your `.gitignore` file to prevent unnecessary conflicts.
Mint
0. Add the [R.swift](https://github.com/mac-cain13/R.swift) library to your project
1. Add `mac-cain13/R.swift` to your [Mintfile](https://github.com/yonaskolb/Mint#mintfile) and run `mint bootstrap` to install this package without linking it globally (recommended)
2. 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
3. Drag the new `Run Script` phase **above** the `Compile Sources` phase, expand it and paste the following script:
```bash
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
```
4. Add `$SRCROOT/R.generated.swift` to the "Output Files" of the Build Phase
5. Uncheck "Based on dependency analysis" so that R.swift is run on each build
6. 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 **uncheck** `Copy 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](http://brew.sh). 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](https://github.com/tomasharkema).
Manually
0. Add the [R.swift](https://github.com/mac-cain13/R.swift) library to your project
1. [Download](https://github.com/mac-cain13/R.swift/releases) a R.swift release, unzip it and put it into your source root directory
2. 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
3. Drag the new `Run Script` phase **above** the `Compile Sources` phase, expand it and paste the following script:
```bash
"$SRCROOT/rswift" generate "$SRCROOT/R.generated.swift"
```
4. Add `$SRCROOT/R.generated.swift` to the "Output Files" of the Build Phase
5. Uncheck "Based on dependency analysis" so that R.swift is run on each build
6. 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 **uncheck** `Copy items if needed`
_Screenshot of the Build Phase can be found [here](Documentation/Images/ManualBuildPhaseExample.png)_
_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!