Hello,
I was merrily programming along when all of a sudden I realised that stringWithFormat had a slightly different way of formatting strings to C's printf, even though the documentation appears to say they both work in pretty much the same way.
In C with printf:
In Objective-C with stringWithFormat::
I'd much rather have stringWithFormat: behave like printf, in that it doesn't require the arguments to actually be inside the string...
Is this a bug, and has anyone else come across this? In any case, is there a quick and easy method to overcome this problem? One thing I thought was just to use printf and something like stringWithCString, but the problem with this is that I'm using %@ as two of the arguments (I only used integers as an example), and obviously printf doesn't support this.
Any ideas?
Edit: Also, forgot to say; the string is dynamic (obtained from a strings file)—the only thing I know for sure are three arguments, and not all three have to be used as part of the string!
I was merrily programming along when all of a sudden I realised that stringWithFormat had a slightly different way of formatting strings to C's printf, even though the documentation appears to say they both work in pretty much the same way.
In C with printf:
Code:
printf("%1$d %3$d",1,2,3);
// Prints "1 3"
In Objective-C with stringWithFormat::
Code:
NSLog([NSString stringWithFormat:@"%1$d %3$d",1,2,3]);
// Prints "1 2"
I'd much rather have stringWithFormat: behave like printf, in that it doesn't require the arguments to actually be inside the string...
Is this a bug, and has anyone else come across this? In any case, is there a quick and easy method to overcome this problem? One thing I thought was just to use printf and something like stringWithCString, but the problem with this is that I'm using %@ as two of the arguments (I only used integers as an example), and obviously printf doesn't support this.
Any ideas?
Edit: Also, forgot to say; the string is dynamic (obtained from a strings file)—the only thing I know for sure are three arguments, and not all three have to be used as part of the string!