Hi, please see the following code:
import Foundation
let s = String(format: "%1$@ %3$@", "Hello", "Bye", "World")
print(s)
It outputs Hello Bye
, but I expected it output Hello World
. I tested it with latest version of macOS and Xcode.
Apple's doc refers to IEEE printf specification about "n$" feature. The spec didn't talk about what's the expected behavior in above case. So I tested the following code on Linux:
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("%1$s %3$s", "Hello", "Bye", "World");
exit(0);
}
It gave a warning and output Hello World
(the result expected by me):
format argument 2 unused before used argument 3 in $-style format
So I think this is a bug?
Background: I'm using String(format:)
to generate localized text. Due to the difference between languages, in language A I need all parameters to generate the text, but in language B I only need some of the parameters. As a result, language B's format string doesn't access all parameters and hence exposes the issue.
I wonder if anyone ran into the issue before and how do you work around it? It seems what I'm looking for is a way to reference the unused parameter in language B's format string but doesn't actually show it in the final result. Perhaps a format specifier to show 0 characters of a string?
Thanks for any suggestion.