簡體   English   中英

R.Swift - 無法調用非函數類型 StringResource 的值

[英]R.Swift - Cannot call value of non-function type StringResource

任何有使用 R.Swift 經驗的人可以提供幫助嗎? 我正在嘗試合並我們項目中使用大量R.string.localizable的代碼,這樣它們的出現就不會散布在整個代碼中,並且字符串(和圖像)不需要一次又一次地重新實例化。 為此,我想添加靜態惰性結構,如下所示:

private struct Text
{
    static let text1 = R.string.localizable.text1()
    static let text2 = R.string.localizable.text2()
    static let text3 = R.string.localizable.text3()
    static let text4 = R.string.localizable.text4
}

這適用於簡單的字符串,但是當任何這些字符串資源需要參數時就會出現問題,例如text4因此被聲明為String Resource而不是String

R.generated.swift:

static let text4 = Rswift.StringResource(key: "some.username", tableName: "Localizable", bundle: R.hostingBundle, locales: ["en", "ja"], comment: nil)

當需要text4並且我嘗試通過我的 Text 結構使用它時,我收到錯誤消息:

_userNameLabel?.text = Text.text4(userName, String.Empty)

錯誤: Cannot call value of non-function type StringResource

有沒有辦法使這項工作?

您可以使用

static func text4(_ value: Int) -> String {
     R.string.localizable.text4(value)
}

並調用Text.text4(10)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM