NSString stringWithFormat 少参数 [英] NSString stringWithFormat less parameters
问题描述
我们需要格式化一个字符串,但对于某些本地化,我们不会输出所有参数.但是输出的参数比传递的少好像不行:
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);
输出
String = <1111>
虽然我输出了第二个参数.这是错误还是功能?
although i output the second parameter. Is this a bug or a feature?
推荐答案
根据相关行业标准,IEEE 规范:
当使用编号参数规范时,指定第 N 个参数要求所有前导参数,从第一个到第 (N-1) 个,指定格式字符串.
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.
<小时>
这意味着换句话说,您必须在字符串格式化程序中的某个位置使用第一个 %1$@
参数,然后才能使用第二个参数 - 因此,这根本不是错误.
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.
这篇关于NSString stringWithFormat 少参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!