Using NSString stringWithFormat: with a string from NSDictionary
I'm getting an NSString from a dictionary that needs to have a variable integer, somethin开发者_如何学运维g 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.
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.
You can access it using this
NSString *string = [NSString stringWithFormat:[dictionary objectForKey:@"string"],YOURINTNUMBER];
It will work
精彩评论