why does this NSLog code print zero's for the

2019-08-06 21:49发布

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

2条回答
Viruses.
2楼-- · 2019-08-06 22:05

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);
查看更多
甜甜的少女心
3楼-- · 2019-08-06 22:06

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)).

查看更多
登录 后发表回答