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.swift
The installation steps (here is mainly about the usecocoapods
To install)
In yourpodfile
Add to fileR.swift
pod 'R.swift'
Copy the code
The terminal executionpod install
The command
Adding Related Configurations
- Click on add
- Place the file in the following format
compile source
andcheck pods
between
- 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+B
Compile the project, will be automatically generated in the project directoryR.generated.swift
file
willR.generated.swift
Drag files into the project, remember not to checkCopy items if needed
R.swift
Specific 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.swift
methods
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.