1

We need to format a string, but for some localisations we won't output all parameters. But it seems that it doesn't work to output less parameters than passed:

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

Outputs

String = <1111>

although i output the second parameter. Is this a bug or a feature?

wutzebaer
  • 14,365
  • 19
  • 99
  • 170
  • It is unclear what you are doing and trying to achieve in your question. Please clarify it. – rptwsthi Jan 07 '16 at 14:40
  • you have to use both parameters, you can't just use the second – Wain Jan 07 '16 at 14:42
  • according to the industrial standard: _"When numbered argument specifications are used, specifying the Nth argument __requires that all the leading arguments__, from the first to the (N-1)th, __are specified in the format string__."_ ([source](http://pubs.opengroup.org/onlinepubs/009695399/functions/printf.html)) – holex Jan 07 '16 at 14:42

1 Answers1

3

according to the related industrial standard, IEEE specification:

When numbered argument specifications are used, specifying the Nth argument requires that all the leading arguments, from the first to the (N-1)th, are specified in the format string.


which means in other words, you must use the first %1$@ parameter in your string formatter somewhere before you address to use the second one – so, it is not a bug at all.

holex
  • 23,961
  • 7
  • 62
  • 76
  • ok means it is impossible to show only the second parameter? – wutzebaer Jan 07 '16 at 14:46
  • according to the current standards, yes, that way would not work, I'm afraid. – holex Jan 07 '16 at 14:47
  • thanks... can you imagine another generic solution? Is there another function which supports it? – wutzebaer Jan 07 '16 at 15:01
  • you may create a category on `NSString` for handle such scenarios, but nothing official pops into my mind about that at this time. – holex Jan 07 '16 at 15:07
  • If you have some special texts that make no sense to say out in other locale (like special suffix of mother-in-law) then ensure that the argument itself is empty string in that other locale, then you can have the argument specification anywhere in format string. – Öö Tiib Aug 21 '23 at 06:43