R.swift + SwiftUI

Shota Ioramashvili
TBC Engineering

--

Accessing various resources within the codebase may be pretty unpleasant and unsafe. For example, if we want to access UIimage, UIColor, UIFont, or NSLocalizedString, they all use string-based initializers 😔. We may initially type the correct names, but there will be no compile-time check for it. For that reason, there will be a big problem in the future when we remove/rename/move some resources. It could cause unexpected results and even crushes. It would be nice to have a static code generator that will make everything safe and easy to maintain.

Fortunately, there is an excellent solution for it. R.swift will make your resources fully typed, compile-time checked and autocompleted. After installing R.swift into your project, you can use the R-struct to access resources. If the struct is outdated, build and R.swift will correct any missing/changed/added resources.

Let’s compare native and R.swift solutions, and after that, we can completely forget how we managed to access resources before R.swift.

There is no comparison 🤭. R.swift makes everything way better, and most importantly, safe!

Let’s go through the integration process with the help of Cocoapods:

  1. Add pod 'R.swift' to your Podfile 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:
    "$PODS_ROOT/R.swift/rswift" generate "$SRCROOT/R.generated.swift"
  4. Add $TEMP_DIR/rswift-lastrun to the "Input Files" and $SRCROOT/R.generated.swift to the "Output Files" of the Build Phase.
  5. Build your project. In Finder, you will now see a R.generated.swift in the $SRCROOTfolder, drag the R.generated.swift files into your project and uncheck Copy items if needed.
  6. Add the *.generated.swift pattern to your .gitignore file to prevent unnecessary conflicts.
The screenshot of the run Run Script Phase. Image from R.swift GitHub page.

R.swift currently supports these types of resources:

  • Images
  • Fonts
  • Resource files
  • Colors
  • Localized strings
  • Storyboards
  • Segues
  • Nibs
  • Reusable cells

and Runtime validation if all:

  • images used in storyboards and nibs are available
  • named colors used in storyboards and nibs are available
  • view controllers with storyboard identifiers can be loaded
  • custom fonts can be loaded
  • localization string keys are equally translated

It's time to talk about SwiftUI. As you may already know, resources generated by R.swift will return UIKit types(UIImage, UIColor, UIFont, etc.). There is no way to ask R.swift to create resources that SwiftUI will natively use. Also, many apps still could not afford to use SwiftUI, but one place where even legacy projects could use it is the iOS 14 widget target. The installation will be the same as described above. The only difference will be using the widget target for the installation's second step instead of the main app target. There will be zero problems because we can have a dedicated R.swift generated file for each target.

Let's concentrate on the image, color, and font resources. We will create the necessary extensions for each resource and start using them inside SwiftUI views.

R.swift provides particular types for each resource. ImageResource, ColorResource, and FontResource. We have added the extensions for each of them to return the native SwiftUI type. Now we will use them inside SwiftUI views and compare them with native implementation:

Now everything is safe and easy to use. Autocompletion really helps, especially when we like asset catalogs organized and namespaced. Or we have dedicated localization files for each feature. Overall readability and swiftness are through the roof 🤗.

R.swift is powerful and easy to use. It will definitely make you and your team members more confident to move things around, refactor and maintain codebase health daily. At TBC bank, we are using it in both of our in-house applications(TBC Business and TBC Bank) and highly recommend it.

For more information about R.swift and its capabilities, please visit the R.swift Github page. 🚀

--

--