R.swift用法

2019-10-10  本文已影响0人  BigBossZhu

R.swift使用和介绍

R.swift可以获取强类型、自动完成的资源,如图像、字体和段落完全类型化。更少的强制转换和猜测方法将返回什么编译时检查,运行时不再有导致应用程序崩溃的错误字符串自动完成,再也不用怀疑图片名字是不是复制错了。默认会在编译时生成所有资源文件的对应文件,可以直接通过代码访问到

1. Images:图片R.image访问

普通写法:

let settingsIcon = UIImage(named: "settings-icon")
let gradientBackground = UIImage(named: "gradient.jpg")

R.swift写法:

let settingsIcon = R.image.settingsIcon()
let gradientBackground = R.image.gradientJpg()

2. fonts:字体R.font访问

普通写法:

let lightFontTitle = UIFont(name: "Acme-Light", size: 22)

R.swift写法:

let lightFontTitle = R.font.acmeLight(size: 22)

3. Resource files:资源文件R.swif访问

普通写法:

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. Colors:颜色R.color访问

普通写法:

view.backgroundColor = UIColor(named: "primary background")

R.swift写法:

view.backgroundColor = R.color.primaryBackground()

5. Localized strings:本地化文字中英文切换

普通写法:

let welcomeMessage = NSLocalizedString("welcome.message", comment: "")

R.swift写法:

let welcomeMessage = R.string.localizable.welcomeMessage()

6. Storyboards:sb和sb内控制器R.storyboard访问

普通写法:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialTabBarController = storyboard.instantiateInitialViewController() as? UITabBarController
let settingsController = storyboard.instantiateViewController(withIdentifier: "settingsController") as? SettingsControllerSettingsController

R.swift写法:

let storyboard = R.storyboard.main()
let initialTabBarController = R.storyboard.main.initialViewController()
let settingsController = R.storyboard.main.settingsController()

7. Segues:withIdentifier改为R.segue.overviewController访问

普通写法:

// Trigger segue with:
performSegue(withIdentifier: "openSettings", sender: self)

// And then prepare it:
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写法:

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

// And then prepare it:
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
  }
}

8. Nib:R.nib访问,nib名字,创建实例,初始化,nib数组获取第一个元view全部使用R.nib

普通写法

let nameOfNib = "CustomView"
let customViewNib = UINib(nibName: "CustomView", bundle: nil)
let rootViews = customViewNib.instantiate(withOwner: nil, options: nil)
let customView = rootViews.first 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)

9. Reusable table view cells:Nib创建的复用cell.通过R.nib访问

普通写法

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.reuseIdentifier.textCell, for: indexPath)!
    textCell.mainLabel.text = "Hello World"
    return textCell
  }
}

10. Reusable collection view cells:Nib创建可复用collectionViewCell和上面访问类似

普通写法

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)!
    cell.configureCell("Item \(indexPath.item)")
    return cell
  }
}

以后在项目中经常使用这种写法

上一篇下一篇

猜你喜欢

热点阅读