为什么这个 NSLog 代码打印值的零?
为什么这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所有这些值都有 CGFloat 类型,但您试图将它们打印为 int 。只需将
%d
替换为%f
即可。PS Apple 开发人员偶尔需要打印 CGRect,因此他们想出了一些方便的方法。尝试 NSLog(@"currFrame 尺寸:%@", NSStringFromCGRect(currFrame))。
All these values have
CGFloat
type but you're trying to print them asint
s. Just replace%d
with%f
.P.S. Apple devs have to print
CGRect
s occasionally so they came up with some handy methods. TryNSLog(@"currFrame dimensions: %@", NSStringFromCGRect(currFrame))
.因为您使用的是
%d
因此将数字格式化为整数。尝试使用%f
或%lf
代替:Because you are using
%d
hence formatting the number as integers. Try using%f
or%lf
instead: