使用 %@ 转换说明符和字段宽度时,Cocoa 的 printf 样式格式是否无法按预期工作?

发布于 2024-08-26 09:41:48 字数 256 浏览 5 评论 0原文

我对类似 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Smile简单爱 2024-09-02 09:41:48

%@ 仅适用于 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 %@.

且行且努力 2024-09-02 09:41:48

%@ 格式说明符只是被对象的描述所替换。它不进行任何截断或填充。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文