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.

4
  • possible duplicate of Passing an array to a function with variable number of args in Swift Commented Apr 2, 2015 at 1:20
  • I saw this post, but it didn't resolve my problem. Commented Apr 2, 2015 at 1:23
  • How about this one: stackoverflow.com/questions/24110853/… 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. Commented Apr 2, 2015 at 7: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. Commented Apr 2, 2015 at 11:29

2 Answers 2

26

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)
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

This does not correctly transform localized string.
Some explaining comment for the downvote would be extremely helpful. If anything is wrong here, let me know so that I can try to fix it.
@BorutTomazin: I have added an example (which is the example from the NSString documentation for localizedStringWithFormat. Please let me know what is wrong here. Can you give a concrete example?
It works for translation, but does not work for plurals. I've modified your method a bit to make this work for plurals also. Thanks!
this is crashing on swift 4.0.
-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

3 Comments

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
When you pass args to getLocalizeWithParams( ), you can use that for any function call within that function. Nothing wrong with that.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.