views:

74

answers:

2

I'm getting an NSString from a dictionary that needs to have a variable integer, something like:

"You have %i objects."

How do I put the calculated integer value into the string? I would like to do something like

NSString *string = [NSString stringWithFormat:[dictionary objectForKey:@"string"]

But I don't know how to pass in an argument to stringWithFormat when the %i is tucked away in the dictionary.

Note: I can work around this by using stringByReplaceOccurenceOfString, but I'd like to know if it's possible to do it in the above way.

+3  A: 

You can pass a comma separated list of arguments to methods like stringWithFormat:

NSString *string = [NSString stringWithFormat:
    [dictionary objectForKey:@"string"] , 7 , 8 , 9];

Only the 7 would be used in your sample format string.

drawnonward
Thanks! This is what I was doing, and this turned out to be one of those times where the problem was somewhere else entirely.
Jon
+1  A: 

You can access it using this

  NSString *string = [NSString stringWithFormat:[dictionary objectForKey:@"string"],YOURINTNUMBER];

It will work

Suriya