1

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
Greg
  • 34,042
  • 79
  • 253
  • 454
  • 1
    The answers below are correct, but in addition you should have a look at the method NSStringFromRect() to help with easier printing of CGRect's – keno Mar 10 '11 at 00:57

2 Answers2

6

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

hoha
  • 4,418
  • 17
  • 15
2

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);
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • thanks - is there no formatter character you can use that would intelligently work out how to best format it based on type? i.e. make it a little simpler – Greg Mar 10 '11 at 00:58
  • oh - I just tried with %f and it didn't work - got the following line as output "currFrame dimensions:x/y/height/width = 0.000000 / 0.000000 / 0.000000 / 0.000000" – Greg Mar 10 '11 at 01:03
  • Have you considered that the values might actually be 0? Have you confirmed that they aren't? – Cory Kilger Mar 10 '11 at 01:18
  • oh - I've changed the 1st parameter passed in from "0" to a "0.0" and this seems to have fixed things - would be good if there was a generic formatting character you could use that would work for anything, e.g. int or float etc – Greg Mar 10 '11 at 01:33