My understanding of printf-like format strings is that you can prefix any conversion specifier with a minimum field width. This does not seem to work for Cocoa’s %@
specifier.
Example:
NSLog(@"'%5@'", @"foo");
NSLog(@"'%5s'", [@"foo" UTF8String]);
Output:
… 'foo'
… ' foo'
Is this the intended behavior?
Oh yes - printf depends on the format string to determine the size and type of the variable to fetch next. When the format string is wrong it may try to fetch a variable that isn't even there, with all consequences that may have.
Any printf format/argument mismatch will cause erroneous output, so you cannot rely on anything once you do that.
String Format Specifier %s The %s format specifier is implemented for representing strings. It is used in the printf() function for printing a string stored in the character array variable.
%@ is only for objective-c object. Thus, the field width will be invalid, if the object is not NSString.
I didn't know that %5@ is formatted to be the same as %@.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With