Strong typed, autocompleted resources like images, fonts and segues in Swift projects
Get strong typed, autocompleted resources like images, fonts and segues in Swift projects
It makes your code that uses resources:
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!
Autocompleted images:
Compiletime checked images:
This is only the beginning, check out more examples!
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.
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()
:
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).
Demo Video: Install R.swift in Xcode with SPM
https://github.com/mac-cain13/R.swift
and click “Add Package”.RswiftGenerateInternalResources
. (Screenshot)R
struct should be available in your code, use auto-complete to explore all static references.Note: The first build you might need to approve the new plugin by clicking the build error warning you about the new plugin.
On your CI server you can’t explicitly allow the build plugin to run, so you need to disable plugin validation to be able to build without user interaction:
defaults write com.apple.dt.Xcode IDESkipPackagePluginFingerprintValidatation -bool YES
before Xcode starts building.On Xcode Cloud you can add a custom build script in ci_scripts/ci_post_clone.sh
with this line that Xcode will run.
dependencies: [
.package(url: "https://github.com/mac-cain13/R.swift.git", from: "7.0.0")
]
.target(
name: "Example",
dependencies: [.product(name: "RswiftLibrary", package: "R.swift")],
plugins: [.plugin(name: "RswiftGeneratePublicResources", package: "R.swift")]
)
R
struct should be available in your code, use auto-complete to explore all static referencespod 'R.swift'
to your Podfile and run pod install
TARGETS
, click the Build Phases
tab and add a New Run Script Phase
by clicking the little plus icon in the top leftRun Script
phase above the Compile Sources
phase and below Check Pods Manifest.lock
, expand it and paste the following script:"$PODS_ROOT/R.swift/rswift" generate "$SRCROOT/R.generated.swift"
$SRCROOT/R.generated.swift
to the “Output Files” of the Build PhaseR.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
Tip: Add the *.generated.swift
pattern to your .gitignore
file to prevent unnecessary conflicts.
TARGETS
, click the Build Phases
tab and add a New Run Script Phase
by clicking the little plus icon in the top leftRun Script
phase above the Compile Sources
phase, expand it and paste the following script:"$SRCROOT/rswift" generate "$SRCROOT/R.generated.swift"
$SRCROOT/R.generated.swift
to the “Output Files” of the Build PhaseR.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
Tip: Add the *.generated.swift
pattern to your .gitignore
file to prevent unnecessary conflicts.
We’ll love contributions, read the contribute docs for info on how to report issues, submit ideas and submit pull requests!
R.swift is created by Mathijs Kadijk and Tom Lokhorst released under a MIT License.