使用 %@ 转换说明符和字段宽度时,Cocoa 的 printf 样式格式是否无法按预期工作?
我对类似 printf 的格式字符串的理解是,您可以为任何转换说明符添加最小字段宽度的前缀。这似乎不适用于 Cocoa 的 %@
说明符。 示例:
NSLog(@"'%5@'", @"foo");
NSLog(@"'%5s'", [@"foo" UTF8String]);
输出:
… 'foo'
… ' foo'
这是预期的行为吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
%@ 仅适用于 Objective-C 对象。因此,如果对象不是 NSString,则字段宽度将无效。
我不知道 %5@ 的格式与 %@ 相同。
%@ 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.