Swift-使用 R.swift 优雅的使用资源文件

57 篇文章 1 订阅
9 篇文章 0 订阅

R.swift 让 Swift 更方便快捷安全的使用资源文件, 一旦学会使用, 我保证你会爱上它

R.swift GitHub 地址


R.swift 特点:

  • 每当项目build时,R.swift开始运行
  • 这个文件根据项目里的资源文件自动在 R.generated.swift 文件中按照类型生成结构体
  • 强类型,无需类型判断和转换,自动返回对应类型
  • 支持多种资源类型
  • 自动完成,无需猜测图像名称,避免资源名称拼写错误

安装

  1. 添加 pod 'R.swift' 到你的 Podfile 文件中, 然后运行 pod install

  2. 在项目中: 点击项目名称, 选择 TARGETS, 点击 Build Phases ,在点击左上角 + 号添加 New Run Script Phase
    这里写图片描述

  3. 打开并复制下面 "$PODS_ROOT/R.swift/rswift" generate "$SRCROOT"到黑色输入框中

  4. 拖动这个脚本在 Check Pods Manifest.lock 之下

这里写图片描述
4. Build 项目, 在 $SRCROOT-folder目录下找到 R.generated.swift 文件 , 拖拽 R.generated.swift 文件到项目中,并不勾选 Copy items if needed
这里写图片描述


R.swift 的具体使用


一、图片-Images

原始用法
let settingsIcon = UIImage(named: "settings-icon")
使用 R.swift
let settingsIcon = R.image.settingsIcon()

二、字体-Fonts

原始用法
let lightFontTitle = UIFont(name: "Acme-Light", size: 22)
使用 R.swift
let settingsIcon = R.image.settingsIcon()

三、文件-Files

原始用法
let plistURL = Bundle.main.url(forResource: "book", withExtension: "plist")
let jsonPath = Bundle.main.path(forResource: "data", ofType: "json")
使用 R.swift
let plistURL = R.file.bookPlist()
let jsonPath = R.file.DataJson.path()

四、颜色-Colors

R.swift 对于颜色是通过(.clr)文件获取颜色
(.clr)文件具体详情请看上篇博客(Swift-颜色设置技巧和(.clr)文件的创建和使用
R.swift 通过 R.color.xxx 使用颜色

原始用法
label.textColor = UIColor(red: 0.3, green: 0.3, blue: 0.3, alpha: 1.0)
使用 R.swift

Colors are extracted from the *.clr files that are in your Xcode project

label.textColor = R.color.appColors.textColor()

五、故事版-Storyboards

原始用法
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabBarController = storyboard.instantiateInitialViewController() as? UITabBarController
let settingsController = storyboard.instantiateViewController(withIdentifier: "settingsController") as? SettingsController
使用 R.swift
let storyboard = R.storyboard.main()
let tabBarController = R.storyboard.main.initialViewController()
let settingsController = R.storyboard.main.settingsController()

六、Segues

原始用法
performSegue(withIdentifier: "openSettings", sender: self)
使用 R.swift
performSegue(withIdentifier: R.segue.overviewController.openSettings, sender: self)

七、Nibs

原始用法
let nib = UINib(nibName: "ToolBar", bundle: nil)
let toolBar = nib.instantiate(withOwner: nil, options: nil).first as? ToolBar
使用 R.swift
let toolBar  = R.nib.toolBar.firstView(owner: self)
let toolBar2 = R.nib.toolBar.secondView(owner: self)

八、Reusable identifier / table view cells

原始用法
class FaqAnswerController: UITableViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    let textCellNib = UINib(nibName: "TextCell", bundle: nil)
    tableView.register(textCellNib, forCellReuseIdentifier: "TextCellIdentifier")
  }

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let textCell = tableView.dequeueReusableCell(withIdentifier: "TextCellIdentifier", for: indexPath) as! TextCell
    textCell.mainLabel.text = "Hello World"
    return textCell
  }
}
使用 R.swift
class FaqAnswerController: UITableViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    tableView.register(R.nib.textCell)
  }

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let textCell = tableView.dequeueReusableCell(withIdentifier: R.nib.textCell.identifier, for: indexPath)!
    textCell.mainLabel.text = "Hello World"
    return textCell
  }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值