ios - How to skip data passing into [NSString stringWithFormat:] -



ios - How to skip data passing into [NSString stringWithFormat:] -

consider below code:

int count = 1; nsstring* format = count == 1 ? @"one %2$@" : @"%1$d %2$@s"; nsstring* result = [nsstring stringwithformat: format, count, @"bread"];

what count not 1, result valid:

2 breads 4 breads

however count 1, causes exc_bad_access

nslog(@"%@", [nsstring stringwithformat:@"one %2$@", 1, @"bread"]);

xcode compiler complains upper code:

data argument not used format string

i know reason error. approach(dynamic format may skips data) useful if works.

is there workaround this?

[nsstring stringwithformat:] not back upwards positional parameters. looks bug in [nsstring stringwithformat:].

one workaround (hack) utilize plain printf functions , convert result nsstring:

char *format = count == 1 ? "one %2$s" : "%1$d %2$ss"; char *tmp; asprintf(&tmp, format, count, "bread"); nsstring *result = [nsstring stringwithutf8string:tmp]; free(tmp);

but proper solution create "localizable.strings" file language plural rules, described in "handling noun plurals , units of measurement" in internationalization , localization guide.

see "string localization" documentation , examples.

ios objective-c xcode nsstring format

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -