为什么这个 NSLog 代码打印值的零?

发布于 2024-10-21 06:56:54 字数 524 浏览 6 评论 0原文

为什么这个 Objective-C 代码打印 0 的值,就像在调试器中一样,我可以看到它们有非 0 值:

代码

CGRect currFrame = label.frame;                 
currFrame.origin.y = currVertPos;
currFrame.origin.x = 0;
currFrame.size.height = expectedLabelSize.height;
currFrame.size.width = maxWidt  h;
NSLog(@"  currFrame dimensions:x/y/height/width = %d / %d / %d / %d", 0, currVertPos, expectedLabelSize.height, maxWidth);

打印内容

currFrame dimensions:x/y/height/width = 0 / 0 / 0 / 0

Why does this objective-c code print 0's for the values, where as in debugger I can see they have non-0 values:

Code

CGRect currFrame = label.frame;                 
currFrame.origin.y = currVertPos;
currFrame.origin.x = 0;
currFrame.size.height = expectedLabelSize.height;
currFrame.size.width = maxWidt  h;
NSLog(@"  currFrame dimensions:x/y/height/width = %d / %d / %d / %d", 0, currVertPos, expectedLabelSize.height, maxWidth);

What is Printed

currFrame dimensions:x/y/height/width = 0 / 0 / 0 / 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

末が日狂欢 2024-10-28 06:56:54

所有这些值都有 CGFloat 类型,但您试图将它们打印为 int 。只需将 %d 替换为 %f 即可。

PS Apple 开发人员偶尔需要打印 CGRect,因此他们想出了一些方便的方法。尝试 NSLog(@"currFrame 尺寸:%@", NSStringFromCGRect(currFrame))。

All these values have CGFloat type but you're trying to print them as ints. Just replace %d with %f.

P.S. Apple devs have to print CGRects occasionally so they came up with some handy methods. Try NSLog(@"currFrame dimensions: %@", NSStringFromCGRect(currFrame)).

就此别过 2024-10-28 06:56:54

因为您使用的是 %d 因此将数字格式化为整数。尝试使用 %f%lf 代替:

NSLog(@"  currFrame dimensions:x/y/height/width = %f / %f / %f / %f", 0, currVertPos, expectedLabelSize.height, maxWidth);

Because you are using %d hence formatting the number as integers. Try using %f or %lf instead:

NSLog(@"  currFrame dimensions:x/y/height/width = %f / %f / %f / %f", 0, currVertPos, expectedLabelSize.height, maxWidth);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文