LoginSignup
10
5

More than 5 years have passed since last update.

R.Swiftでは文字リソースで「%@」を使うと、その部分を引数にとる関数を自動生成する

Posted at

うまいタイトル思いつかなかった。
tipsです。

以下のように@リテラルで文中の動的な部分を定義しておくと、R.swiftでは @リテラル部分を引数に取って該当部分に挿入した文字列を返してくれるstatic funcが自動生成されます。

Strings.strings
"receivedDate" = "受け取り日時:%@";
R.generated.swift

/// Value: 受け取り日時:%@
static func receivedDate(_ value1: String) -> String {
    return String(format: NSLocalizedString("receivedDate", tableName: "Strings", bundle: R.hostingBundle, comment: ""), locale: R.applicationLocale, value1)
}
使う側.swift

Strings.receivedDate("\(date.year)/\(date.month)/\(date.day)")

わぁ便利!知らなかった!

10
5
0

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
10
5