What does this format specifier% 1 $@ % 2 $ d mean in object c

I worked with format specifiers, but they were generic, for example %d or %@ , but today in the tutorial I saw these % 1$@ %2$d and did not understand what they represent.
It was a calculator to use them in this expression: stack = [NSString stringWithFormat:@"% 1$@ %2$d",stack,number];

+5
source share
1 answer

Numbers are positional parameters. The parameters that follow the format string are placed in the string depending on their position in the parameter list. The first parameter goes to slot% 1, the second goes to slot% 2, etc. The goal is to deal with languages ​​where the order of terms / words / etc can change from your default. You cannot change the order of the parameters at runtime, but you can make sure that the parameters are in the right place on the line.

Example

 NSLog(@"% 1$@ , % 2$@ ", @"Red", @"Blue"); NSLog(@"% 2$@ , % 1$@ ", @"Red", @"Blue"); 

Output

 Red, Blue Blue, Red 

Note that the format string has changed, but the options are in the same order.

+5
source

Source: https://habr.com/ru/post/1235176/


All Articles