2016-04-06 51 views
2

以下语句表示在目标c中定义宏。如何使用swift编程语言创建宏作为函数

据我所知,在Swift中不支持宏,所以我必须使用函数,所以你可以帮助和提供什么将是以下语句的swift代码?

#define LOCAL_STRING(KEY, VALUE, ...) [NSString localizedStringWithFormat:NSLocalizedString(@KEY, [email protected]), ##__VA_ARGS__] 
+0

http://stackoverflow.com/questions/24325477/how-to-use-a-objective-c-define-从-迅速 – swiftBoy

回答

3

尝试使用字符串扩展(答案为这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: []) 

附:在这个变量示例中的空数组应该没问题,如果在键/值中没有格式参数