I'm wondering the best way to approach the following problem,
My app downloads from a website a string with looks something like this:
Code:
"I have 3 parameters. The first is %@, the second is %@ and the third is %@"
I know I can using stringWithFormat like this to pass three parameters:
Code:
NSString * downloadedString = @"I have 3 parameters. The first is %@, the second is %@ and the third is %@";
NSString * formattedString = [NSString stringWithFormat:downloadedString,firstValue,secondValue,thirdValue];
What I'm not sure how to do is pass a varying number of parameters/arguments to stringWithFormat.
Let's say for instance that the string that's being downloaded changed to:
Code:
"4 params. 1 = %@, 2 = %@, 3 = %@ and 4 = %@"
Instead of rewriting the code, it would be good if the app could just deal with it, working out there is 4 arguments this time, and passing 4 to it.
I can tell the app how many arguments there are going to be, I can also tell the app what those arguments should be, what I'm uncertain on is how code for the changes.
Something like:
Code:
NSArray * arrayContainingArguments = [[NSArray alloc] initWithObjects:@"first",@"second",@"third",@"forth",nil];
NSString * downloadedString = @"4 params. 1 = %@, 2 = %@, 3 = %@ and 4 = %@";
NSString * finalString = [NSString stringWithFormat:downloadedString,arrayContainingArguments];
[arrayContainingArguments release];
Any ideas?
Tom.