这似乎是一个非常基本的问题,但我似乎无法在任何地方找到答案:( 我能够在Objective C中做到这一点,但我在Swift中卡住了。
我需要做的:
- 获取整数值
- 将其格式化为本地化字符串
- 使用
stringWithFormat
等效方法将该值注入到另一个字符串中(由于另一个字符串也被本地化,因此在下面的简化示例中未显示)
在Objective C中如何轻松完成-这很有效:
// points is of type NSNumber *
NSNumberFormatter *formatter = [NSNumberFormatter new];
formatter.locale = [NSLocale currentLocale];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
NSString *ptsString = [formatter stringFromNumber:points];
NSString *message = [NSString stringWithFormat:@"You've earned %@ points", ptsString];
我在 Swift 中尝试实现这个最佳方案,但是最后一行出现编译错误:
// points is of type Int
let formatter = NSNumberFormatter()
formatter.locale = NSLocale.currentLocale()
formatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
let ptsString = formatter.stringFromNumber(points)!
let message = String(format: "You've earned %@ points", arguments: ptsString)
在Xcode中,我在最后一行代码遇到了以下错误:
"Cannot convert value of type 'String' to expected argument type '[CVarArgType]'"
(在我的实际代码中,我想要插入点值的消息本身也是本地化的,但是我简化了这个例子,因为在两种情况下都出现了完全相同的错误。)
我错过了什么..?
非常感谢任何帮助,
Erik
let message = String(format: "You've earned %@ points", arguments: ptsString)
中,只需要删除arguments:
即可... 也就是说,它应该是let message = String(format: "You've earned %@ points", ptsString)
。 - 0x416e746f6e