类似的(Objective- )C,不能将可变参数列表 直接传递给另一个函数。您必须创建一个CVaListPointer
(C中的Swift等效项va_list
),并调用一个函数,其中 需要参数CVaListPointer
。
因此,这可能是你在找什么:
extension String {
func getLocalizeWithParams(args : CVarArgType...) -> String {
return withVaList(args) {
NSString(format: self, locale: NSLocale.currentLocale(), arguments: $0)
} as String
}
}
withVaList()
创建从给定的参数列表 一个CVaListPointer
并调用该指针作为参数关闭。
实施例(从NSString
文档):
let msg = "%@: %f\n".getLocalizeWithParams("Cost", 1234.56)
print(msg)
输出,用于美国的区域设置:
Cost: 1,234.560000
输出为德语区域:
Cost: 1.234,560000
更新:作为夫特3/4一个可以传递的参数
String(format: String, locale: Locale?, arguments: [CVarArg])
直接:
[传递的阵列,以与夫特ARGS的可变数量的函数的可能重复](http://stackoverflow.com/questions/24024376/passing-an-array-to-a-function-with-variable-number-of-args-in-swift) – newacct 2015-04-02 01:20:44
我看到这篇文章,但它didn'解决我的问题。 – ViTUu 2015-04-02 01:23:56
这个如何:http://stackoverflow.com/questions/24110853/passing-in-variable-number-of-args-from-one-function-to-another-in-swift这是完全一样的问题。它被标记为上述的一个副本;这就是我使用那个的原因。 – newacct 2015-04-02 07:27:13