R.swift-优雅地引用项目资源

栏目: Swift · 发布时间: 6年前

内容简介:R.swift是一个高效引入iOS资源的框架,避免了使用字符串引入资源文件导致程序崩溃的尴尬。 目前R.swift支持到Swift4.0版本使用代码的方式来引入资源:示例:

R.swift是一个高效引入iOS资源的框架,避免了使用字符串引入资源文件导致程序崩溃的尴尬。 目前R.swift支持到Swift4.0版本

优势

使用代码的方式来引入资源:

  • 类型完整 : 减少推断和转换方法返回值的类型
  • 编译时检查 : 不会再因为不正确的字符串导致App产生运行时崩溃
  • 自动补全 : 不再需要输入图片名称

示例:

之前:

let image = UIImage(named: "imageName")
复制代码

R.swift:

let image = R.image.imageName()
复制代码

功能

使用 R.swift 之后,可以使用 R 结构体来获取资源文件。如若资源文件发生改变, R.swift 会正确的清除\改变\增加资源。

R.swift 目前支持如下几种资源:

  • Images
  • Fonts
  • Resource files
  • Colors
  • Localized String
  • StoryBoard
  • Segues
  • Nibs
  • Reuseable cells

接入方式

CocoaPods

  1. 添加 pod R.swiftPodfile 文件中之后终端执行 pod install
  2. Xcode 配置:在当前项目的 targets 中选择 Build phrase ,点击 + 号选择添加 New Run Script Phase ,
  3. Run Script 移动至 Compile sources 之上, Check Pods Manifest.lock 之下。在 Run Script 中添加: "$PODS_ROOT/R.swift/rswift" "$SRCROOT" ,
  4. 编译你的项目,在 Finder 中你会看到 R.generated.swiftPod 文件中,将该文件拖动至项目中,切记千万 不要 勾选 Copy items if needed ,
  5. 新增\删除\修改资源文件之后都需要重新 command+B 编译项目,保证正确引用。

使用方式

1、Images

之前:

let png = UIImage(named: "png")
let jpg = UIImage(named: "jpg.jpg")
复制代码

R.swift:

let png = R.image.png()
let jpg = R.imgae.jpgJpg()
复制代码

2、Custom fonts

之前:

let customFont = UIFont(name: "Acme-Light", size: 22)
复制代码

R.swift:

let customFont = R.font.acmeLight(size: 22)
复制代码

3、Resource files

之前 :

let jsonURL = Bundle.main.url(forResource: "seed-data", withExtension: "json")
let jsonPath = Bundle.main.path(forResource: "seed-data", ofType: "json")
复制代码

R.swift:

let jsonURL = R.file.seedDataJson()
let jsonPath = R.file.seedDataJson.path()
复制代码

4、Localized Strings

之前:

let welcomeMessage = NSLocalizedString("welcome.message", comment: "")
let settingsTitle = NSLocalizedString("title", tableName: "Settings", comment: "")
let welcomeName = String(format: NSLocalizedString("welcome.withName", comment: ""), locale: NSLocale.current, "Alice")
let progress = String(format: NSLocalizedString("copy.progress", comment: ""), locale: NSLocale.current, 4, 23)
复制代码

R.swift:

let welcomeMessage = R.string.localizable.welcomeMessage()
let settingsTitle = R.string.settings.title()
let welcomeName = R.string.localizable.welcomeWithName("Alice")
let progress = R.string.localizable.copyProgress(completed: 4, total: 23)
复制代码

5、Segues

之前:

performSegue(withIdentifier: "openSettings", sender: self)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let settingsController = segue.destination as? SettingsController,
       let segue = segue as? CustomSettingsSegue, segue.identifier == "openSettings" {
      segue.animationType = .LockAnimation
      settingsController.lockSettings = true
    }
  }
复制代码

R.swift:

performSegue(withIdentifier: R.segue.overviewController.openSettings, sender: self)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if let typedInfo = R.segue.overviewController.openSettings(segue: segue) {
    typedInfo.segue.animationType = .LockAnimation
    typedInfo.destinationViewController.lockSettings = true
  }
复制代码

6、Nibs

之前:

let nameOfNib = "CustomView"
let customViewNib = UINib(nibName: "CustomView", bundle: nil)
let rootViews = customViewNib.instantiate(withOwner: nil, options: nil)
let customView = rootViews[0] as? CustomView

let viewControllerWithNib = CustomViewController(nibName: "CustomView", bundle: nil)
复制代码

R.swift:

let nameOfNib = R.nib.customView.name
let customViewNib = R.nib.customView()
let rootViews = R.nib.customView.instantiate(withOwner: nil)
let customView = R.nib.customView.firstView(owner: nil)

let viewControllerWithNib = CustomViewController(nib: R.nib.customView)
复制代码

7、Reusable table view cells

UITableView

之前:

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
  }
}
复制代码

UICollectionView

之前:

class RecentsController: UICollectionViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    let talkCellNib = UINib(nibName: "TalkCell", bundle: nil)
    collectionView?.register(talkCellNib, forCellWithReuseIdentifier: "TalkCellIdentifier")
  }

  override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TalkCellIdentifier", for: indexPath) as! TalkCell
    cell.configureCell("Item \(indexPath.item)")
    return cell
  }
}
复制代码

R.swift:

class RecentsController: UICollectionViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    collectionView?.register(R.nib.talkCell)
  }

  override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: R.reuseIdentifier.talkCell, for: indexPath)! // 此处跟tableView的使用方式还是有很大区别
    cell.configureCell("Item \(indexPath.item)")
    return cell
  }
}
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

尽在双11:阿里巴巴技术演进与超越

尽在双11:阿里巴巴技术演进与超越

阿里巴巴集团双11技术团队 / 电子工业出版社 / 2017-4 / 79

“双 11”,诞生于杭州,成长于阿里,风行于互联网,成就于新经济,贡献于全世界。 从 2009 年淘宝商城起,双 11 已历经八年。每年的双 11 既是当年的结束,又是走向未来的起点。技术的突破创新,商业模式的更替交互,推动着双 11 迈步向前。 《尽在双11——阿里巴巴技术演进与超越》是迄今唯一由阿里巴巴集团官方出品、全面阐述双 11 八年以来在技术和商业上演进和创新历程的书籍。内容......一起来看看 《尽在双11:阿里巴巴技术演进与超越》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换