EXC_BAD_ACCESS in statement block in Switch impact

2019-05-14 12:00发布

I have a strange situation. I get an exception on the following code on the close 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 double class member. And imageViewPosition is a CGPoint. When I run it like this, I get a EXC_BAS_ACCESS exception.

In the following cases I get no exception and everything works as expected. I don't understand why that is the case.

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

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

b) When I place the case statement within curly brackets 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 that the answers to the a) and b) options will be different. I had this already earlier in another situation with a switch, so I want to understand what I'm doing wrong. Thanks.

0条回答
登录 后发表回答