所以我有以下代码:
游戏场景:
Rule = Level / 10
let StrRule = String(Rule) //for example Level = 24 -> Rule = 2
let RegelString = String(format: NSLocalizedString("RegelText", comment: ""), StrRule)
//Give the String out in an UILabel as text
RegelLabel.text = RegelString
这就是我的 Localized.strings 的样子:
"RegelText" = "You lose %d goals if you miss.";
所以我开始游戏,我的等级
是24。所以规则
应该是2。
所以我构建并运行,但是当我启动应用程序时,他向我展示了一些疯狂的数字。
标签就像:如果你错过了,你就会失去 1314063968 个目标。
为什么不起作用?
最佳答案
%d
用于数字。 StrRule
显然不是一个数字。您打算使用%s
吗? RegelText
应该是这样的:
"RegelText" = "You lose %s goals if you miss.";
或者只使用Rule
而不将其转换为字符串。代码可能是这样的:
Rule = Level / 10
let RegelString = String(format: NSLocalizedString("RegelText", comment: ""), Rule)
在这种情况下,RegelText
仍将使用 %d
。
此外,我没有看到您对 Level/10
的结果进行四舍五入。我知道整数除以整数会得到整数,而不是小数。但为了安全起见,我认为你应该将 Level/10
放在 round
或 floor
函数中。
关于string - 带有变量的 NSLocalizedString 对我不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36113890/