19

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?

Thanks advance.

ViTUu
  • 1,204
  • 11
  • 21
  • possible duplicate of [Passing an array to a function with variable number of args in Swift](http://stackoverflow.com/questions/24024376/passing-an-array-to-a-function-with-variable-number-of-args-in-swift) – newacct Apr 02 '15 at 01:20
  • I saw this post, but it didn't resolve my problem. – ViTUu Apr 02 '15 at 01:23
  • How about this one: http://stackoverflow.com/questions/24110853/passing-in-variable-number-of-args-from-one-function-to-another-in-swift It's exactly the same as your problem. It was marked as a duplicate of the above one; that's why I used that one. – newacct Apr 02 '15 at 07:27
  • The issue is same, but he has two functions constructed by self and he can modify to receive array and fix, in my issue I have one owner function and need use a system function.. It is different. My luck is someone help me with a good solution and work fine. – ViTUu Apr 02 '15 at 11:29

2 Answers2

25

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.

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.

Example (from the NSString documentation):

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/5 one can pass the arguments to

String(format: String, locale: Locale?, arguments: [CVarArg])

directly:

extension String {
    func getLocalizeWithParams(_ args : CVarArg...) -> String {
        return String(format: self, locale: .current, arguments: args)
    }
}
James Bedford
  • 28,702
  • 8
  • 57
  • 64
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
-2

I believe you're using NSString.localizedStringWithFormat(self, args) incorrectly. Otherwise nothing wrong with using args to call another function.

If you look below, you need to specify the format as NSString as the first argument: NSString.localizedStringWithFormat(format: NSString, args: CVarArgType...)

This SO question explains how to use it in Swift: iOS Swift and localizedStringWithFormat

Community
  • 1
  • 1
anasimtiaz
  • 321
  • 3
  • 9
  • Thank you, but I know how work localizedStringWithFormat, I need know how pass args... params to another function like localizedStringWithFormat args... understand? Sry my english – ViTUu Apr 01 '15 at 20:40
  • When you pass args to getLocalizeWithParams( ), you can use that for any function call within that function. Nothing wrong with that. – anasimtiaz Apr 01 '15 at 20:57
  • Args... in **getLocalizeWithParams()** is an _Array_, my array can contain **n** items, because this i need pass to **NSString.localizedStringWithFormat** a **n** values, and when i try this way I do, it pass one array not a many values to **NSString.localizedStringWithFormat** – ViTUu Apr 01 '15 at 21:01