最終確認日

R.swift はがし

R.swift を使わなくてもよくなってきた。Warningが多いので剥がすことにした。

使っていたところ

  • R.image

  • R.color

  • R.nib

  • R.reuseIdentifier

  • R.string.localizable

  • R.font

大枠

項目 🟠R.swift 🟢R.swiftはがし
画像 R.image.hogeImage() UIImage(resource: .hogeImage)
R.color.lightPink() UIColor(resource: .lightPink)
nib R.nib.hogeView UINib(nibName: "HogeView", bundle: nil)
UITableView
UICollectionView
R.reuseIdentifier.hogeCollectionViewCell HogeCollectionViewCell.className
(クラス名じゃなくても全然ok)
フォント R.font.hogeBold() UIFont(name: "hogeBold.otf", size: 20)
ローカライズ R.string.localizable.hogeText() String(localized: "Hoge.Text", defaultValue: "Hoge")
String Catalogs を使う

画像

🟠 R.swift

R.image.hogeImage()

🟢 R.swift はがし

UIImage(resource: .hogeImage)

🟠 R.swift

R.color.lightPink()

🟢 R.swift はがし

UIColor(resource: .lightPink)

xib

ViewController系

🟠 R.swift

HogeViewController(nib: R.nib.hogeViewController)

🟢 R.swift はがし

HogeViewController()

// もしくは
HogeViewController(nibName: HogeViewController.nibName, bundle: nil)
import UIKit

extension UIViewController {

    static var nibName: String {
        String(describing: self)
    }
}

カスタムView系

🟠 R.swift

R.nib.hogeView

🟢 R.swift はがし

UINib(nibName: HogeView.nibName, bundle: nil)
import UIKit

extension UIView {

    static var nibName: String {
        String(describing: self)
    }
}

UITableView

🟠 R.swift

// セルの登録
tableView.register(R.nib.hogeTableViewCell)

// セルの再利用
let cell = tableView.dequeueReusableCell(withIdentifier: R.reuseIdentifier.hogeTableViewCell, for: indexPath)!

🟢 R.swift はがし

// セルの登録
tableView.register(UINib(nibName: HogeTableViewCell.nibName, bundle: nil), forCellReuseIdentifier: HogeTableViewCell.className)

// セルの再利用
guard let cell = tableView.dequeueReusableCell(withIdentifier: HogeTableViewCell.className, for: indexPath) as? HogeTableViewCell else { fatalError("cellの設定が正しくありません。") }
import Foundation

extension NSObject {

    class var className: String {
        return String(describing: self)
    }

    var className: String {
        return type(of: self).className
    }
}

UICollectionView

🟠 R.swift

// セルの登録
collectionView.register(R.nib.hogeCollectionViewCell)

// セルの再利用
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: R.reuseIdentifier.hogeCollectionViewCell, for: indexPath)!

🟢 R.swift はがし

// セルの登録
collectionView.register(UINib(nibName: HogeCollectionViewCell.nibName, bundle: nil), forCellReuseIdentifier: HogeCollectionViewCell.className)

// セルの再利用
guard let cell = collectionView.dequeueReusableCell(withIdentifier: HogeCollectionViewCell.className, for: indexPath) as? HogeCollectionViewCell else { fatalError("cellの設定が正しくありません。") }
import Foundation

extension NSObject {

    class var className: String {
        return String(describing: self)
    }

    var className: String {
        return type(of: self).className
    }
}

フォント

🟠 R.swift

R.font.hogeBold(size: 20)

🟢 R.swift はがし

UIFont(name: "hogeBold.otf", size: 20)

こういうのでも

enum FontResource {
    static let hogeBold = "hogeBold.otf"
}

UIFont(name: FontResource.hogeBold, size: 10)

テキスト

R.swift をはがし、さらに Localizable.strings から String Catalogs ( Localizable.xcstrings) に移行。

コード上のテキスト

Localizalbe.strings

// en
"Hoge.title" = "Hoge";

// ja
"Hoge.title" = "ほげ";

🟠 R.swift

R.string.localizable.hogeTitle()

🟢 R.swift はがし

String(localized: "Hoge.title", defaultValue: "Hoge")

日本語は String Catalogs で追加。

xib上のテキスト

ここからは R.swift は関係なし。

🟠 Localizable.strings

これまではEasy XIB and Storyboard Localizationの記事のやり方で xib上の xibLocKeyLocalizable.stringsのキーを設定する方法を使っていた。

R.swift はがし-20241220192129704

🟢 String Catalogs を使う

ローカライズを行いたい .xibファイルを開く。

Show the File inspector > Localization > Localize... を選択。
Use String Catalog にチェックを入れて Localize を行う。

R.swift はがし-20241220192247565

次のようなファイルが生成される。

R.swift はがし-20241220192524425

HomeViewController (Strings) (HomeViewController.xcstrings) を開く。

HomeViewController.xib 内のテキストが自動で抽出されるので、ローカライズを行う。

R.swift はがし-20241220192656798

自動抽出されたラベルのローカライズを行いたくない場合には、 Mark as "Don't Translate" を選択しておいた。

R.swift はがし-20241220193015115

必要な箇所のみローカライズを行い ✅ 状態にする。

R.swift はがし-20241220193123727

参考

String Catalogsの紹介 - WWDC23 - ビデオ - Apple Developer
String Catalogsの概要と妥当な運用方針

サイトアイコン
公開日
更新日