Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C : Cannot initialize UIViewController in switch case?

Tags:

objective-c

Objective-C question : Cannot initialize UIViewController in switch case?

switch(anIntegerIndex) {
  case 1:
    UIViewController *vc = [[UIViewController alloc] init];
    break;
  default:
    break;
}

the above has compile error in the UIViewController line. I'm using iOS 5 SDK + xCode 4.2

like image 931
Raptor Avatar asked Dec 13 '22 07:12

Raptor


1 Answers

It's a common C restriction for switch statements --

You cannot declare local variables inside an individual case unless you put them inside {} brackets.

Simplest thing to do is to put the UIViewController *vc; declaration ahead of the switch and just put vc = [[whatever.. inside the case.

like image 50
Hot Licks Avatar answered Jan 18 '23 22:01

Hot Licks