This is the fourth day of my participation in the Gwen Challenge.More article challenges

What is a cornerstone library?

There are two main elements to making an App:

  • To get the data

  • Drive pages through data

Maybe your App has no or few network requests and you don’t need Alamofire.

Maybe your App’s UI isn’t too complicated, and a simple XIB and storyboard will do the trick.

But in the current App, image resources, string resources, as an App developer, you have to use them.

For example, the traditional way to get an image resource is to write:

let image = UImage(named: "saber")
Copy the code

The biggest drawback to this is that saber is a hard-coded string that is manually typed, and if something goes wrong, the interface will throw an exception.

In the development, we need to try to avoid this kind of hard coding, how to effectively replace this kind of hard coding expression way with efficient and safe way, by the protagonist of this time –R.swift.

Unifying all resources, referencing resources in a modern way, and using it in a project won’t take your App to the next level, but it will give your code extreme comfort.

let image = R.image.saber()
Copy the code

The same Ex curry sticks taste completely different lol.

Cornerstone libraries are the ones you can’t avoid having to use, and R.swift is exactly that.

R.swift

What is R, short for Resource, let’s take a look at some official examples:

Before using the r.swift function:

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")
Copy the code

Using the r.swift function:

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")
Copy the code

All resources after the function, the writing process to go wrong all difficult, need special attention is that the last one involving the internationalization functions. R.s tring localizable. WelcomeWithName (” Arthur Dent “), Arthur Dent the string need to specify, This can be handled by using info.strings when doing internationalization.

R. Swift currently supports the following resource file management:

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

Basically covers resource management in most apps.

Installation and use

The installation

R.swift is extremely comfortable to use, but it does require a bit more work to install than other third-party libraries, and it’s worth the trouble.

1. Add ‘R.swift’ to the project Podfile and run Pod Install. 2. See the figure below. Add script:

3. Move the script between Compile Sources Phase and Check Pods manifest.lock, as shown below:

4. Add script:

Under the shell, add the column “$PODS_ROOT/R.s wift rswift” generate “. R. gutierrez enerated $SRCROOT/swift”

Add $TEMP_DIR/rswift-lastrun with the + sign in the input Files

In the Output Files through the + add $SRCROOT/r. gutierrez enerated. Swift

5. Run add r.enerated. swift:

After step 4, command + B and you’ll find the r.enerated. swift file in the root directory of the project:

Drag this file into the project and uncheck Copy Items if needed

This completes the r.swift installation.

use

Each time a new resource file is added, run command + B once, so that the r.enerated. swift file updates the newly added resource file, using only R. For reference.

For more usage, see the examples written above, as well as the official documentation

How about tomorrow weekend?

The most afraid of the weekend more text, because as a dad, rest is not their own, I strive to do not hydrological, at least some knowledge points, continue tomorrow, everyone refuels.