Weird Switch error in Obj-C

后端 未结 7 545
天命终不由人
天命终不由人 2020-11-29 00:39

I have this switch statement in my code:

switch(buttonIndex){
      case 0:
         [actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];
           


        
7条回答
  •  醉梦人生
    2020-11-29 01:34

    Well this isn't a proper answer, as I don't know why you see that error, but a better solution than to move the declaration outside the switch block is to make explicit scopes within the individual cases of the switch block. That usually solves any such problem with switch statements, which are a bit weird in that the case statements share scope.

    So:

    switch (foo) {
       case 0: {
          // notice explicit scope here
          break;
       }
       default: {
          // here as well
       }
    }
    

提交回复
热议问题