2016-04-06 70 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: []) 

附:在這個變量示例中的空數組應該沒問題,如果在鍵/值中沒有格式參數