Advanced Localization with Omission of Arguments in Xcode

爱⌒轻易说出口 提交于 2019-11-28 12:22:22

This is not a bug. Numbered arguments are not part of the C standard, but part of IEEE Std 1003.1, which says the following (emphasis mine):

The format can contain either numbered argument conversion specifications (that is, "%n$" and "*m$"), or unnumbered argument conversion specifications (that is, % and * ), but not both. The only exception to this is that %% can be mixed with the "%n$" form. The results of mixing numbered and unnumbered argument specifications in a format string are undefined. 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.

Looks like a bug to me. I think you should file a bug.

CFString's formatting engine is independent from fprintf's so there could be some differences. For instance,

printf("a %3$s\n", "b", "c", "d"); // prints "a d"
NSLog(@"a %3$s\n", "b", "c", "d"); // prints "a b"

You need to supply all previous specifiers because the width of an argument doesn't need to be fixed, e.g

printf("%2$llx %1$llx\n", 1LL, 2LL); // prints "2 1"
printf("%2$llx\n", 1LL, 2LL);        // prints "200000000" !!
NSLog(@"%2$llx %1$llx\n", 1LL, 2LL); // prints "2 1"
NSLog(@"%2$llx\n", 1LL, 2LL);        // prints "1"

iPhone OS's printf skips 4 bytes on 1 missing specifier, and CFString's formatter skips 0 bytes.


The solutions are:

  1. Rearrange your indices, e.g.

    "Check out the %4$@ %1$@ in %2$@: %3$@"
    "Hör Dir mal %1$@ in %2$@ an: %3$@";
    

    or

  2. use the format

    [@"%1$10p%2$10p%3$10p%4$10p" stringByAppendingString:frmt]
    

    to force all arguments to be used, and then chop out the first 40 characters with -substringFromIndex:, or

  3. Convert all ObjC objects into C strings (char*) and use snprintf.

  4. Write your own formatting engine.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!