3

Following statement represents defining macro in objective c.

As I know macro is not supported in Swift so I have to use function for that, so can you help and provide me what will be the swift code for the following statement?

#define LOCAL_STRING(KEY, VALUE, ...) [NSString localizedStringWithFormat:NSLocalizedString(@KEY, !@VALUE), ##__VA_ARGS__]
Bhargav Kukadiya
  • 418
  • 7
  • 17

1 Answers1

3

Try using a String extension (the answer for which 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)
      }
}

So then you can do something like:

"KeyNameHere".localizedStringWithVariables("some default value", vars: [])

p.s. the empty array in this vars example should be fine if you have no format arguments in the key/value

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215