Examples
On this page you'll find examples of the kind of resources R.swift supports and how you can use them. We aim to keep this page up to date and complete so this should be a overview of all possibilities.
Runtime validation
Call R.validate() to call all validation methods that R.swift generates, this will check:
- If all images used in storyboards and nibs are available
- If all view controllers with storyboard identifiers can be loaded
- If all custom fonts can be loaded
The R.validate() method will throw a detailed error about the problems that occur. Note that this method will always perform checks, even in release builds. It's recommended that validation is done in a testcase.
Example testcase
try R.validate()
} catch {
XCTFail(error)
}
Images
R.swift will find both images from Asset Catalogs and image files in your bundle.
Vanilla
let gradientBackground = UIImage(named: "gradient.jpg")
With R.swift
let gradientBackground = R.image.gradientJpg()
Custom fonts
Vanilla
With R.swift
Resource files
Vanilla
let jsonPath = Bundle.main.path(forResource: "seed-data", ofType: "json")
With R.swift
let jsonPath = R.file.seedDataJson.path()
Colors
Vanilla
label.textColor = UIColor(red: 0.3, green: 0.3, blue: 0.3, alpha: 1.0)
With R.swift
label.backgroundColor = R.color.appColors.backgroundColor()
label.textColor = R.color.appColors.textColor()
There are some points to keep in mind when using Color palettes, see About Colors
Localized strings
Vanilla
let settingsTitle = NSLocalizedString("title", tableName: "Settings", comment: "")
// Formatted strings
let welcomeName = String(format: NSLocalizedString("welcome.withName", comment: ""), locale: NSLocale.current, "Alice")
// Stringsdict files
let progress = String(format: NSLocalizedString("copy.progress", comment: ""), locale: NSLocale.current, 4, 23)
With R.swift
let welcomeMessage = R.string.localizable.welcomeMessage()
let settingsTitle = R.string.settings.title()
// Functions with parameters are generated for format strings
let welcomeName = R.string.localizable.welcomeWithName("Alice")
// Functions with named argument labels are generated for stringsdict keys
let progress = R.string.localizable.copyProgress(completed: 4, total: 23)
Storyboards
Vanilla
let initialTabBarController = storyboard.instantiateInitialViewController() as? UITabBarController
let settingsController = storyboard.instantiateViewController(withIdentifier: "settingsController") as? SettingsControllerSettingsController
With R.swift
let initialTabBarController = R.storyboard.main.initialViewController()
let settingsController = R.storyboard.main.settingsController()
Segues
Vanilla
performSegue(withIdentifier: "openSettings", sender: self)
// And then prepare it:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let settingsController = segue.destination as? SettingsController,
let segue = segue as? CustomSettingsSegue, segue.identifier == "openSettings" {
segue.animationType = .LockAnimation
settingsController.lockSettings = true
}
}
With R.swift
performSegue(withIdentifier: R.segue.overviewController.openSettings, sender: self)
// And then prepare it:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let typedInfo = R.segue.overviewController.openSettings(segue: segue) {
typedInfo.segue.animationType = .LockAnimation
typedInfo.destinationViewController.lockSettings = true
}
}
Tip: Take a look at the SegueManager library, it makes segues block based and is compatible with R.swift.
Nibs
Vanilla
let customViewNib = UINib(nibName: "CustomView", bundle: nil)
let rootViews = customViewNib.instantiate(withOwner: nil, options: nil)
let customView = rootViews[0] as? CustomView
let viewControllerWithNib = CustomViewController(nibName: "CustomView", bundle: nil)
With R.swift
let customViewNib = R.nib.customView()
let rootViews = R.nib.customView.instantiateWithOwner(nil)
let customView = R.nib.customView.firstView(owner: nil)
let viewControllerWithNib = CustomViewController(nib: R.nib.customView)
Reusable table view cells
Vanilla
override func viewDidLoad() {
super.viewDidLoad()
let textCellNib = UINib(nibName: "TextCell", bundle: nil)
tableView.register(textCellNib, forCellReuseIdentifier: "TextCellIdentifier")
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let textCell = tableView.dequeueReusableCellWithIdentifier("TextCellIdentifier", forIndexPath: indexPath) as! TextCell
textCell.mainLabel.text = "Hello World"
return textCell
}
}
With R.swift
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(R.nib.textCell)
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let textCell = tableView.dequeueReusableCellWithIdentifier(R.nib.textCell.identifier, forIndexPath: indexPath)!
textCell.mainLabel.text = "Hello World"
return textCell
}
}
Reusable collection view cells
Vanilla
override func viewDidLoad() {
super.viewDidLoad()
let talkCellNib = UINib(nibName: "TalkCell", bundle: nil)
collectionView?.register(talkCellNib, forCellWithReuseIdentifier: "TalkCellIdentifier")
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("TalkCellIdentifier", forIndexPath: indexPath) as! TalkCell
cell.configureCell("Item \(indexPath.item)")
return cell
}
}
With R.swift
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.register(R.nib.talkCell)
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(R.reuseIdentifier.talkCell, forIndexPath: indexPath)!
cell.configureCell("Item \(indexPath.item)")
return cell
}
}