Why useR.swift

R.swift can get strongly typed, autocomplete resources such as fully typed images, fonts, and paragraphs. Fewer cast and guesswork methods will return compile-time checks, no more error string auto-completion at runtime that crashed the application, and no more wondering if the image name was copied incorrectly.

R.swiftThe installation steps (here is mainly about the usecocoapodsTo install)

In yourpodfileAdd to fileR.swift
pod 'R.swift'
Copy the code
The terminal executionpod installThe command
Adding Related Configurations
  • Click on add

  • Place the file in the following formatcompile sourceandcheck podsbetween

  • Add items according to the following figure. The content to be added is as follows
"$PODS_ROOT/R.swift/rswift" generate "$SRCROOT/mm/Resources/R.generated.swift"
$TEMP_DIR/rswift-lastrun
$SRCROOT/mm/Resources/R.generated.swift
Copy the code

Adding is done,Command+BCompile the project, will be automatically generated in the project directoryR.generated.swiftfile
willR.generated.swiftDrag files into the project, remember not to checkCopy items if needed

R.swiftSpecific use of

The old way of using it
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")
Copy the code
R.swiftmethods
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

Problems encountered

Error: Failed for unknown Distill failed when r. swift was being imported At first, I thought the picture was duplicated, but after checking it again and again, I found that the picture was not duplicated. Finally, I found that the reader’s MAC was using the test version of the system. After upgrading the system to the official version, there was no such problem.