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

时间:2011-03-10 00:49:34

标签: iphone objective-c ios string-formatting 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

2 个答案:

答案 0 :(得分:6)

所有这些值均为CGFloat类型,但您尝试将其打印为int。只需将%d替换为%f

P.S。 Apple开发者必须偶尔打印CGRect,以便他们想出一些方便的方法。试试NSLog(@"currFrame dimensions: %@", NSStringFromCGRect(currFrame))

答案 1 :(得分:2)

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

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