开发者

Does Cocoa’s printf-style formatting not work as expected when using %@ conversion specifier and field width?

开发者 https://www.devze.com 2022-12-23 17:22 出处:网络
My understanding of printf-like format strings is that you can prefix any conve开发者_如何学运维rsion specifier with a minimum field width. This does not seem to work for Cocoa’s %@ specifier.

My understanding of printf-like format strings is that you can prefix any conve开发者_如何学运维rsion 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?


%@ 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 %@.


The %@ format specifier just gets replaced by the object's description. It doesn't do any truncation or padding.

0

精彩评论

暂无评论...
验证码 换一张
取 消