Search code examples
iphoneobjective-ciosstring-formattingnslog

why does this NSLog code print zero's for the values?


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

Solution

  • 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);