Cocoa s printf formatting not working properly when using the% @conversion specifier and field width?

My understanding of printf strings is that you can prefix any conversion specifier with a minimum field width. This does not work for Cocoa s %@ specifier. Example:

 NSLog(@"'% 5@ '", @"foo"); NSLog(@"'%5s'", [@"foo" UTF8String]); 

Output:

 … 'foo' … ' foo' 

Is this the intended behavior?

+6
printf cocoa string-formatting
source share
2 answers

% @ is for the objective-c object only. Therefore, the field width will be invalid if the object is not an NSString.

I did not know that% 5 @ is formatted in the same way as% @.

+1
source share

The% @ format specifier is simply replaced by the description of the object. He does not perform any truncations or additions.

0
source share

All Articles