I have a simple problem. I tried search in many blogs about this question but all site return how function in swift work, but I need this case.
我有一个简单的问题。我尝试在很多博客上搜索这个问题,但是所有的网站都返回如何快速工作,但是我需要这个案例。
My custom function is:
我的自定义函数是:
func getLocalizeWithParams(args:CVarArgType...)->String {
return NSString.localizedStringWithFormat(self, args); //error: Expected expression in list of expressions
}
How I do to pass my args to other system function with args?
如何将args传递给其他系统函数?
Thanks advance.
谢谢你提前。
2 个解决方案
#1
18
Similar as in (Objective-)C, you cannot pass a variable argument list directly to another function. You have to create a CVaListPointer
(the Swift equivalent of va_list
in C) and call a function which takes a CVaListPointer
parameter.
类似于(Objective-)C,不能将变量参数列表直接传递给另一个函数。您必须创建一个CVaListPointer(在C中与va_list等价),并调用一个接受CVaListPointer参数的函数。
So this could be what you are looking for:
这可能就是你要找的
extension String {
func getLocalizeWithParams(args : CVarArgType...) -> String {
return withVaList(args) {
NSString(format: self, locale: NSLocale.currentLocale(), arguments: $0)
} as String
}
}
withVaList()
creates a CVaListPointer
from the given argument list and calls the closure with this pointer as argument.
withVaList()从给定的参数列表中创建一个CVaListPointer,并使用该指针作为参数调用闭包。
Example (from the NSString
documentation):
示例(来自NSString文档):
let msg = "%@: %f\n".getLocalizeWithParams("Cost", 1234.56)
print(msg)
Output for US locale:
输出为我们地区:
Cost: 1,234.560000
Output for German locale:
输出为德国语言环境:
Cost: 1.234,560000
Update: As of Swift 3/4 one can pass the arguments to
更新:Swift 3/4可以将参数传递给。
String(format: String, locale: Locale?, arguments: [CVarArg])
directly:
直接:
extension String {
func getLocalizeWithParams(_ args : CVarArg...) -> String {
return String(format: self, locale: .current, arguments: args)
}
}
#2
0
I believe you're using NSString.localizedStringWithFormat(self, args)
incorrectly. Otherwise nothing wrong with using args to call another function.
我相信你用的是NSString。localizedStringWithFormat(自我,args)不正确。否则,使用args调用另一个函数没有问题。
If you look below, you need to specify the format as NSString as the first argument: NSString.localizedStringWithFormat(format: NSString, args: CVarArgType...)
如果您看下面,您需要将格式指定为NSString作为第一个参数:NSString。localizedStringWithFormat(格式:NSString,arg游戏:CVarArgType…)
This SO question explains how to use it in Swift: iOS Swift and localizedStringWithFormat
这个问题解释了如何在Swift中使用它:iOS Swift和localizedStringWithFormat。
#1
18
Similar as in (Objective-)C, you cannot pass a variable argument list directly to another function. You have to create a CVaListPointer
(the Swift equivalent of va_list
in C) and call a function which takes a CVaListPointer
parameter.
类似于(Objective-)C,不能将变量参数列表直接传递给另一个函数。您必须创建一个CVaListPointer(在C中与va_list等价),并调用一个接受CVaListPointer参数的函数。
So this could be what you are looking for:
这可能就是你要找的
extension String {
func getLocalizeWithParams(args : CVarArgType...) -> String {
return withVaList(args) {
NSString(format: self, locale: NSLocale.currentLocale(), arguments: $0)
} as String
}
}
withVaList()
creates a CVaListPointer
from the given argument list and calls the closure with this pointer as argument.
withVaList()从给定的参数列表中创建一个CVaListPointer,并使用该指针作为参数调用闭包。
Example (from the NSString
documentation):
示例(来自NSString文档):
let msg = "%@: %f\n".getLocalizeWithParams("Cost", 1234.56)
print(msg)
Output for US locale:
输出为我们地区:
Cost: 1,234.560000
Output for German locale:
输出为德国语言环境:
Cost: 1.234,560000
Update: As of Swift 3/4 one can pass the arguments to
更新:Swift 3/4可以将参数传递给。
String(format: String, locale: Locale?, arguments: [CVarArg])
directly:
直接:
extension String {
func getLocalizeWithParams(_ args : CVarArg...) -> String {
return String(format: self, locale: .current, arguments: args)
}
}
#2
0
I believe you're using NSString.localizedStringWithFormat(self, args)
incorrectly. Otherwise nothing wrong with using args to call another function.
我相信你用的是NSString。localizedStringWithFormat(自我,args)不正确。否则,使用args调用另一个函数没有问题。
If you look below, you need to specify the format as NSString as the first argument: NSString.localizedStringWithFormat(format: NSString, args: CVarArgType...)
如果您看下面,您需要将格式指定为NSString作为第一个参数:NSString。localizedStringWithFormat(格式:NSString,arg游戏:CVarArgType…)
This SO question explains how to use it in Swift: iOS Swift and localizedStringWithFormat
这个问题解释了如何在Swift中使用它:iOS Swift和localizedStringWithFormat。