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 ("expected expression before 'NSMutableArray'").
Asked
Active
Viewed 406 times
3 Answers
5
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;
}
}

Richard J. Ross III
- 55,009
- 24
- 135
- 201

Benoît
- 7,395
- 2
- 25
- 30
4
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;
...
}

Vladimir
- 170,431
- 36
- 387
- 313
0
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"

SNR
- 1,249
- 2
- 20
- 38

Grady Player
- 14,399
- 2
- 48
- 76