Wednesday, January 1, 2014

Can’t declare variables inside a switch case – Objective C

If you were under the above impression then the answer is you can with a little bit of change to your code. I’m guessing you are used to the following format,

switch (value){
case value1:
//some code
break;
case value2:
//some code
break;
default:
//some code
break;
}

If you want to declare variables inside a case you need to follow the format below

switch (value){
case value1:{
//some code
//you can declare any variables here
//(notice the additional brackets)
break;
}
case value2:
//some code
break;
default:
//some code
break;
}

No comments: