# 09.R-Swift-安全的资源组织方案forSwift

> 官方链接: <https://github.com/mac-cain13/R.swift> 参考文章:
>
> > <http://swifter.tips/safe-resource/> <https://www.jianshu.com/p/6234e6e79b15>

## 场景

在项目中经常会遇到通过字符串进行的一些资源操作比如图片什么的,维护修改成本较高

* 一般操作

```swift
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")
```

* 使用`R.Swift`

```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")
```

> 是不是显得十分清爽

## 导入`R.Swift`

* `pod 'R.swift'`
* `install`完成后添加 `New Run Script Phase`

  ![](http://ohfpqyfi7.bkt.clouddn.com/15179849878526.jpg)
* 添加脚本 `"$PODS_ROOT/R.swift/rswift" generate "$SRCROOT"`

  ![](http://ohfpqyfi7.bkt.clouddn.com/15179848803178.jpg)
* `Command+B`编译一次后会在目录下生成一个`R.generated.swift`文件,将其拖入工程

  ![](http://ohfpqyfi7.bkt.clouddn.com/15179854353794.jpg)

> 一定不要选中`Copy items if needed` 因为每次编译都会重新生成该文件所以最好在`.gitignore`中添加忽略`*.generated.swift`


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ryukiedev.gitbook.io/wiki/swift/rswift-an-quan-de-zi-yuan-zu-zhi-fang-an-forswift.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
