LoginSignup
65
66

More than 5 years have passed since last update.

さらに便利になったR.swiftの実力を見るがいい

Last updated at Posted at 2016-06-23
1 / 23

R.swiftとは


UIImage(named:"kono_asset_ha_sonzai_shite_masu")

UIImage(named:"kono_asset_ha_sonzai_shite_masu")


実は kono_asset_ha_sonzai_site_masu が正しい


tableView.registerNib(
    UINib(nibName: "ArchiveCell", bundle: nil),
    forCellReuseIdentifier: "AchieveCell")

tableView.registerNib(
    UINib(nibName: "ArchiveCell", bundle: nil),
    forCellReuseIdentifier: "AchieveCell")


CRASH!!


tableView.registerNib(
    UINib(nibName: "ArchiveCell", bundle: nil),
    forCellReuseIdentifier: "AchieveCell")


CRASH!!

実はArchiveCell じゃなくて AchieveCell


こういうのを安全に書けるようにするのが

✨R.swift✨

です


UIImage(named:"kono_asset_ha_sonzai_site_masu")

 ↓

R.image.kono_asset_ha_sonzai_site_masu()

tableView.registerNib(
    UINib(nibName: "AchieveCell", bundle: nil),
    forCellReuseIdentifier: "AchieveCell")

tableView.registerNib(R.nib.AchieveCell)

先行記事

できることについては、こちらの記事に短く纏まっています。

R.swiftを使ってStoryboard名や画像名のTypoを0にする - Qiita


先行記事

できることについては、こちらの記事に短く纏まっています。

R.swiftを使ってStoryboard名や画像名のTypoを0にする - Qiita

対応しているリソースの種別は…


  • StoryBoard
  • Nib
  • Reuse Identifier
  • UIImage
  • Segue
  • Custom Fonts

  • StoryBoard
  • Nib
  • Reuse Identifier
  • UIImage
  • Segue
  • Custom Fonts
  • Localized Strings ←NEW!!
  • Colors ←NEW!!

Localized strings


NSLocalizedString("welcome.message", comment: "")

NSLocalizedString("title",
                  tableName: "Settings",
                  comment: "")

R.string.localizable.welcomeMessage()

R.string.settings.title()

Colors


UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 0.5)
UIColor(red: 0.3, green: 0.3, blue: 0.3, alpha: 1.0)

// Colors are extracted from the *.clr files
// that are in your Xcode project
R.color.appColors.backgroundColor()
R.color.appColors.textColor()

the *.clr files ってなぁに?

IBから作成できるパレット
それをコードからも使えるようにするってこと!

cf.
XCode Tip: Color Palette
https://www.natashatherobot.com/xcode-color-palette/


R.swiftを使うと、

  • typoがなくなる
  • コード補完が掛かる
  • リソースが消えた際にはビルドエラーで検知できる

= 本質的な開発に集中できる


だけではなく、

  • UITableViewやUICollectionViewのCellを管理するためのextensionもついてくる!

R.swiftでUITableViewの扱いがどれだけシンプルになるか、デモプロジェクトを用意しました。


ViewControllerのソースだけ載せると、

class ViewController: UIViewController, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!
    let viewModel = ViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.registerNib(R.nib.colorCell)
        tableView.registerNib(R.nib.sliderCell)
        tableView.registerNib(R.nib.switchCell)
    }
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.datasource.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let dequeuer = tableView.dequeuer(forIndexPath: indexPath)

        switch viewModel.datasource[indexPath.row] {
        case .Color(let color):
            return dequeuer.dequeue(R.reuseIdentifier.colorCell).then {
                $0.color = color
            }

        case .Switch(let on):
            return dequeuer.dequeue(R.reuseIdentifier.switchCell).then {
                $0.uiSwitch.on = on
            }

        case .Slider(let value):
            return dequeuer.dequeue(R.reuseIdentifier.sliderCell).then {
                $0.slider.value = value
            }
        }
    }
}

たったこれだけ
すごくね!?


というわけで R.swiftを使って快適に開発しましょう

65
66
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
65
66