Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Swift, how to insert formatted Int value into String?

This seems like a super basic question, but I just can't seem to find the answer anywhere :-( I am able to do this in Objective C, but I am getting stuck in Swift.

What I need to do:

  1. Take an Integer value
  2. Format it into a localized string
  3. Inject the value into another string using the stringWithFormat equivalent method (since the other string is localized as well, which is not shown in simplified examples below)

How it's easily done in Objective C -- this works:

    // points is of type NSNumber *

    NSNumberFormatter *formatter = [NSNumberFormatter new];
    formatter.locale             = [NSLocale currentLocale];
    formatter.numberStyle        = NSNumberFormatterDecimalStyle;

    NSString *ptsString = [formatter stringFromNumber:points];
    NSString *message   = [NSString stringWithFormat:@"You've earned %@ points", ptsString];

My best attempt at doing this in Swift -- compiler error on last line:

    // points is of type Int

    let formatter         = NSNumberFormatter()
    formatter.locale      = NSLocale.currentLocale()
    formatter.numberStyle = NSNumberFormatterStyle.DecimalStyle

    let ptsString = formatter.stringFromNumber(points)!
    let message   = String(format: "You've earned %@ points", arguments: ptsString)

I'm getting the following error in Xcode on that last line:

"Cannot convert value of type 'String' to expected argument type '[CVarArgType]'"

(In my actual code, the message into which I want to insert the points value is itself localized as well, but I have simplified this example, as I'm getting the exact same error in both cases.)

What am I missing here..?

Thanks so much for any help,

Erik

like image 858
Erik van der Neut Avatar asked Mar 04 '16 08:03

Erik van der Neut


People also ask

How do I assign an Int to a string in Swift?

//convert Integer to String in Swift 3.0 let theIntegerValue :Int = 123 // this can be var also let theStringValue :String = String(theIntegerValue) //convert String to Integere in Swift 3.0 let stringValue : String = "123" let integerValue : Int = Int(stringValue)!

How do I convert numbers to words in Swift?

Open a Swift playground in Xcode and enter the following code: let formatter = NumberFormatter() formatter. numberStyle = . spellOut let number = 87654 let spelledOutNumber = formatter.


2 Answers

Sometimes, you need a little more control - so if you need to have leading zeros, you could use 'stringWithFormat' just like in objective-C

let ptsString = String(format: "%02d", points)
like image 138
Russell Avatar answered Sep 30 '22 17:09

Russell


You need to wrap the arguments in a collection. Like this:

let message   = String(format: "You've earned %@ points", arguments: [ptsString])

You can also use this method:

let message   = "You've earned \(ptsString) points"

Additionally you can create an extension method to do this:

extension String {
    func format(parameters: CVarArgType...) -> String {
        return String(format: self, arguments: parameters)
    }
}

Now you can do this:

let message = "You've earned %@ points".format("test")
let message2params = "You've earned %@ points %@".format("test1", "test2")
like image 30
totiDev Avatar answered Sep 30 '22 15:09

totiDev