开发者

switch/case problem

If i try to create variable in case statement it gives me build erroe Can anyone clue me in why this syntax gets me a build error开发者_Go百科 ("expected expression before 'NSMutableArray'").


Try adding brackets {} in your case statement to be able to declare variables, like this:

switch (my_switch_statement)
{
     case my_switch_case: 
     {
         NSMutableArray *my_switch_array;
     }
}


Assuming you try do something like:

switch (...){
  case someCase:
        NSMutableArray *array = ...
        break;
...
}

c (and so objective-c) does not allow to declare variables inside switch-case statement. If you want to do that you must limit the variable scope by putting your code inside {} block:

switch (...){
  case someCase:{
        NSMutableArray *array = ...
  }
        break;
...
}


generally you will want to declare the variable outside of the scope of the switch, just like any conditional block of code.


NSString * valueString;
int i = 1;
switch(i){
  case 0:
    valueString = @"case 0";
  break;
  case 1:
    valueString = @"case 1";
  break;
  default:
    valueString = @"not case 1 or 0";
  break;
}
//valueString=>@"case 1"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜