0

I tried to switch the following code to a switch statement (You can ignore the comments):

BEFORE

if (self.players == 1) {
    OnePlayerViewController *onePlayerViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OnePlayerViewController"];
    /*onePlayerViewController.dictionary = self.words;
    onePlayerViewController.wordsText = self.wordsText;*/
    onePlayerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:onePlayerViewController animated:YES completion:nil];
} else if (self.players == 2) {
    TwoPlayersViewController *twoPlayersViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"TwoPlayersViewController"];
    /*twoPlayersViewController.dictionary = self.words;
    twoPlayersViewController.wordsText = self.wordsText;*/
    twoPlayersViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:twoPlayersViewController animated:YES completion:nil];
} else if (self.players == 3) {
    ThreePlayersViewController *threePlayersViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ThreePlayersViewController"];
    /*threePlayersViewController.dictionary = self.words;
    threePlayersViewController.wordsText = self.wordsText;*/
    threePlayersViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:threePlayersViewController animated:YES completion:nil];
} else {
    FourPlayersViewController *fourPlayersViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"FourPlayersViewController"];
    /*fourPlayersViewController.dictionary = self.words;
    fourPlayersViewController.wordsText = self.wordsText;*/
    fourPlayersViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:fourPlayersViewController animated:YES completion:nil];
}

AFTER

switch (self.players) {
    case 1:
        OnePlayerViewController *onePlayerViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OnePlayerViewController"];
        /*onePlayerViewController.dictionary = self.words;
        onePlayerViewController.wordsText = self.wordsText;*/
        onePlayerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:onePlayerViewController animated:YES completion:nil];
        break;
    case 2:
        TwoPlayersViewController *twoPlayersViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"TwoPlayersViewController"];
        /*twoPlayersViewController.dictionary = self.words;
        twoPlayersViewController.wordsText = self.wordsText;*/
        twoPlayersViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        break;
    case 3:
        ThreePlayersViewController *threePlayersViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ThreePlayersViewController"];
        /*threePlayersViewController.dictionary = self.words;
        threePlayersViewController.wordsText = self.wordsText;*/
        threePlayersViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:threePlayersViewController animated:YES completion:nil];
        break;

    case 4:
        FourPlayersViewController *fourPlayersViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"FourPlayersViewController"];
        /*fourPlayersViewController.dictionary = self.words;
        fourPlayersViewController.wordsText = self.wordsText;*/
        fourPlayersViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:fourPlayersViewController animated:YES completion:nil];
        break;
}

HOWEVER, the if, else if, and else code works perfectly, but the switch statement gives a lot of errors when run through the compiler. The big one is "Expected expression" that is given on the lines which instantiate the viewcontrollers. I'm guessing that it is expecting an "expression" for the case blah: and that line did not suffice as an "expression" for some odd reason. Because it doesn't seem to register the line and give the view controller an identifier, it gives more errors which mainly include "Use of undeclared identifier 'blahPlayerViewController'; did you mean 'BlahViewController'?". Why does this happen? What can I do to fix this?

ThatGuy
  • 119
  • 10
  • what exactly IS "`self.players`" defined as? – Michael Dautermann Nov 22 '14 at 03:12
  • `self.players` is a NSInteger used to determine the amount of players that are playing. Depending on how may players there are, it will present a different view specialized for that amount of players. – ThatGuy Nov 22 '14 at 03:19

1 Answers1

1

One of the unfortunate things about "switch" statements is that you can't declare variables within them.

You need to declare your variables above and outside of your switch statement.

Something like:

OnePlayerViewController *onePlayerViewController;
TwoPlayersViewController *twoPlayersViewController;

switch (self.players) {
    case 1:
        onePlayerViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OnePlayerViewController"];
        onePlayerViewController.dictionary = self.words;
        onePlayerViewController.wordsText = self.wordsText;
        onePlayerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:onePlayerViewController animated:YES completion:nil];
        break;
    case 2:
        twoPlayersViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"TwoPlayersViewController"];
        twoPlayersViewController.dictionary = self.words;
        twoPlayersViewController.wordsText = self.wordsText;
        twoPlayersViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        break;
Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215