views:

735

answers:

3

C# has syntax that allows you to specify the argument index in a string format specifier, e.g.:

string message = string.Format("Hello, {0}. You are {1} years old. How does it feel to be {1}?", name, age);

You can use arguments more than once and also omit arguments that are provided from being used. Another question mentions the same formatting for C/C++ in the form of %[index]$[format], e.g. %1$i. I have not been able to get NSString to fully respect this syntax, because it does behave well when omitting arguments from the format. The following does not work as expected (EXC_BAD_ACCESS because it attempts to dereference the age parameter as a NSObject*):

int age = 23;
NSString * name = @"Joe";
NSString * message = [NSString stringWithFormat:@"Age: %2$i", name, age];

The positional arguments are respected only if there are no missing arguments from the format (which seems to be an odd requirement):

NSString * message = [NSString stringWithFormat:@"Age: %2$i; Name: %1$@", name, age];

All these calls work properly in OS X:

printf("Age: %2$i", [name UTF8String], age);
printf("Age: %2$i %1$s", [name UTF8String], age);

Is there a way of accomplishing this using NSString in Objective-C / Cocoa? It would be useful for localization purposes.

A: 

After doing more research, it appears Cocoa respects positional syntax in printf. Therefore an alternate pattern would be:

char msg[512] = {0};
NSString * format = @"Age %2$i, Name: %1$s"; // loaded from resource in practice
sprintf(msg, [format UTF8String], [name UTF8String], age);
NSString * message = [NSString stringWithCString:msg encoding:NSUTF8StringEncoding];

However, it would be nice if there was an implementation of this on NSString.

Jason
`sprintf` is not part of Cocoa, it's part of the C standard library, and the implementation of that is `stringWithFormat:`/`initWithFormat:`.
Peter Hosey
Clarifying my previous comment: The Cocoa version is `stringWithFormat:`/`initWithFormat:`. It's a separate implementation (currently, `CFStringCreateWithFormat`) from that of `sprintf` and friends.
Peter Hosey
+5  A: 

NSString and CFString support reorderable/positional arguments.

NSString *string = [NSString stringWithFormat: @"Second arg: %2$@, First arg %1$@", @"<1111>", @"<22222>"];
NSLog(@"String = %@", string);

Also, see the documentation at Apple: Strings Files

Jim Correia
I've updated the question with some clarifications. It appears Cocoa does not respect omitted arguments from the format, which was a side effect of the access violation I was receiving.
Jason
Respecting omitted arguments is not possible because of the way varargs in C work. There is no standard way to know the number of arguments or their size. String parsing handles this by inferring the information from the format specifiers, which requires that the specifiers are actually there.
Ahruman
I understand how va_args works; however, this appears to work as expected: printf("Age: %2$i", [name UTF8String], age); I've tried other printf's with reordered/missing args and they all give the expected output, whereas NSString does not.
Jason
Just to reiterate my findings, then: `stringWithFormat:` supports positional arguments as long as all arguments are specified in the format string.
Jason
A: 

File a bug report (and let us know the bug#).

Dirk

Dirk Theisen