以下语句表示在 objective-c 中定义宏。
据我所知,Swift 不支持宏,所以我必须为此使用函数,所以你能帮我提供以下语句的 swift 代码吗?
#define LOCAL_STRING(KEY, VALUE, ...) [NSString localizedStringWithFormat:NSLocalizedString(@KEY, !@VALUE), ##__VA_ARGS__]
最佳答案
尝试使用字符串扩展(I found here 的答案):
extension String {
func localizedStringWithVariables(value: String, vars: CVarArgType...) -> String {
return String(format: NSLocalizedString(self, tableName: nil, bundle: NSBundle.mainBundle(), value: value, comment: ""), arguments: vars)
}
}
那么你可以这样做:
"KeyNameHere".localizedStringWithVariables("some default value", vars: [])
附注如果键/值中没有格式参数,则此 vars 示例中的空数组应该没问题
关于ios - 如何使用 swift 编程语言将宏创建为函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36442545/