我正在尝试本地化包含 1..2 等范围的字符串。我正在使用String.localizedStringWithFormat
:
func testLocalizableString() -> String {
let lowerBound = 1
let upperBound = 2
return String.localizedStringWithFormat(
NSLocalizedString("Unit.Meters.Range", value:"%d-%dm", comment: ""),
[lowerBound, upperBound]
)
}
但是,我得到了奇怪的结果:“103,413,600-0m”。
如果我只使用一个参数(只是“%dm”,而不是“%d-%dm”)并且只传递一个数字,一切都很好。
我的代码可能有什么问题以及如何使用 CVarArg 参数正确设置可本地化字符串的格式?
最佳答案
第二个参数
String.localizedStringWithFormat(_ format: String, _ arguments: CVarArg...)
是一个可变参数,这意味着您必须传递零个或多个 指定类型的参数,而不是数组:
func testLocalizableString() -> String {
let lowerBound = 1
let upperBound = 2
return String.localizedStringWithFormat(
NSLocalizedString("Unit.Meters.Range", value:"%ld-%ldm", comment: ""),
lowerBound, upperBound
)
}
另请注意,Int
的格式说明符是 %ld
,而不是 %d
。
关于swift - String.localizedStringWithFormat 返回错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41349115/