0

I got stuck with something very stupid: cannot operate with CGFloat getting from view size. Could somebody help me and explain what am I doing wrong:

Here is the code:

 CGFloat containedViewHeight = self.containedView_.frame.size.height;
 NSLog(@"========================   containedViewHeight  ================= %d", containedViewHeight);
 NSLog(@"========================   containedViewHeight1  ================= %d", self.containedView_.frame.size.height);

Here what I got:

 ========================   containedViewHeight  ================= 1587588653

 ========================   containedViewHeight1  ================= 775

Value "775" looks good, but I cant save it to any variable. I also tried something like this:

int offset = containerHeight - (int)(self.containedView_.frame.size.height);

(int)(self.containedView_.frame.size.height) is always equal 0

Please give me a hint of what is wrong...

*** If I use %f as inside log than I got:

========================   containedViewHeight  ================= 0.000000
=======================   containedViewHeight1  ================= 0.000000
Bitlejuce Do
  • 163
  • 3
  • 14

1 Answers1

0

You're passing %d to NSLog's string format. That interprets containedViewHeight as an integer. You want %f.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Thanks for answer, but it does not work eather: – Bitlejuce Do Jan 29 '21 at 15:03
  • ================== containedViewHeight ================= 0.000000 ================= containedViewHeight1 ================= 0.000000 – Bitlejuce Do Jan 29 '21 at 15:04
  • Then I expect that `self.containedView_.frame.size.height` is zero. Why do you believe it's something else? – Rob Napier Jan 29 '21 at 15:13
  • Because the view exists on the screen and I also got another values printed using %d – Bitlejuce Do Jan 29 '21 at 15:15
  • 2
    It existing on screen doesn't mean that *this variable* has the frame you expect at the time you're reading it. It may be resized at a different time, or the view on screen may be a different object. Using `%d` doesn't tell you anything, because it's incorrectly decoding the memory. It doesn't mean "convert this to an int." It means "read this raw memory, and I promise you it is an int." You can double-check this with a debugger. I expect the value is 0. – Rob Napier Jan 29 '21 at 15:18