mac-cain13 /R.swift
Strong typed, autocompleted resources like images, fonts and segues in Swift projects
★ 9,549⑂ 773👁 107⑃ 277● 666▤ 115
+0
日增长
9,549
stars
773
forks
107
watchers
277
prs
666
issues
115
releases
README
R.swift !Platform
_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:
swift
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:
swift
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:
!Autocompleted images
Compiletime checked 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:
- Images
- Fonts
- Resource files
- Colors
- Localized strings
- Storyboards
- Segues
- Nibs
- Reusable cells
- Project
- Entitlements
- Info.plist
Runtime validation with R.validate():
- If all images used in storyboards and nibs are available
- If all named colors used in storyboards and nibs are available
- If all view controllers with storyboard identifiers can be loaded
- If all custom fonts can be loaded
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 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?
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 using SPM (Recommended)
Demo Video: Install R.swift in Xcode with SPM
1. In Project Settings, on the tab "Package Dependencies", click "+", search for https://github.com/mac-cain13/R.swift aTop issues
#777Issue with Xcode Cloud29 comments · 2022-12-05#30Generate warnings when not using R.swift in code where you should14 comments · 2015-05-23#852'Command PhaseScriptExecution failed with a nonzero exit code' when Archiving12 comments · 2023-09-24#805RswiftResources no such module error12 comments · 2023-02-09#610Respect excluded/included source file name environment variable11 comments · 2020-02-26#861String catalogs from WWDC239 comments · 2023-10-21#489Slow build times for R.generated.swift9 comments · 2019-02-15#815Multiple commands produce 'xxx/RswiftGeneratePublicResources/xxx/R.generated.swift'8 comments · 2023-03-14
Latest release
7.8.0 7.8.0 2024-11-05Add support for Xcode 16 file system Folders (https://github.com/mac-cain13/R.swift/pull/921)