Errors With Switch Statement to Present New View

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?


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;
链接地址: http://www.djcxy.com/p/84314.html

上一篇: 在输入语句块时是否会创建一个新的Stack Frame?

下一篇: 开关语句的错误呈现新视角