Objective-C: Can't initialize UIViewController in case of switch?

Objective-C question: Can't initialize a UIViewController in case of a switch?

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

the above compilation error in the UIViewController line. I am using iOS 5 SDK + xCode 4.2

+4
source share
2 answers

This is a general C restriction for switch -

You cannot declare local variables inside a separate case unless you put them in brackets {} .

The easiest way to do this is to place a declaration of UIViewController *vc; before switch and just put vc = [[whatever.. inside the case .

+13
source

Did you try to create an object but did not initialize it?

For instance,

UIViewController * vc = null;

switch (anIntegerIndex) {Case 1: * vc = [[UIViewController alloc] init];

etc.

0
source

Source: https://habr.com/ru/post/1382415/


All Articles