Swift is a strongly typed language, so it makes sense when using it to tweak your tools and code to take advantage of type-checking. This week's nugget goes along that line.
The library R.swift automatically generates static references to resources such as font files and image files so that you don't have to refer to them by string. Example from the README:
let icon = UIImage(named: "settings-icon") let font = UIFont(name: "San Francisco", size: 42) let color = UIColor(named: "indictator highlight") let viewController = CustomViewController(nibName: "CustomView", bundle: nil) let string = String(format: NSLocalizedString("welcome.withName", comment: ""), locale: NSLocale.current, "Arthur Dent")
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 avoids the chance of typing a string wrongly and not catching it before submitting a build to the app store.
Installation is simple:
- Include the pod (
- Add a
Run Scriptphase before
"$PODS_ROOT/R.swift/rswift" generate "$SRCROOT"
- Add the
R.generated.swiftfile to your project