EXC_BAD_ACCESS in the statement block in the effect of the switch on imageViewPoistion

I have a strange situation. I get an exception from the following code in the closing curly brackets of the switch statement

imageViewPosition = self.imageView6.center;
switch(direction) {
   case(1): 
      if (starty-imageViewPosition.y>50) {
         imageViewPosition.y = starty+100;
      } else {
         imageViewPosition.y = starty;
      }
      break;
   case(2): 
      ...
   }         <----- Here i get the exception

starty is a member of a double class. And ImageViewPosition is CGPoint. When I run it like this, I get an EXC_BAS_ACCESS exception.

In the following cases, I get no exception and everything works as expected. I do not understand why this is so.

a) When I add brackets to the if statement, everything works fine:

if ((starty-imageViewPosition.y)>50) {

b) When I put the case statement in braces, everything works fine.

imageViewPosition = self.imageView6.center;
switch(direction) {
   case(1): {
      if (starty-imageViewPosition.y>50) {
         imageViewPosition.y = starty+100;
      } else {
         imageViewPosition.y = starty;
      }
      }
      break;

Why? I expect the answers to options a) and b) to be different. I had this earlier in a different switch situation, so I want to understand what I'm doing wrong. Thank.

+3
source share

All Articles