开关语句的错误呈现新视角

我试图将下面的代码切换到switch语句(您可以忽略注释):

之前

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];
}

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;
}

然而ifelse ifelse代码完美工作,但switch语句在运行编译器时会产生很多错误。 最大的一个就是在实例化视图控制器的行上给出的“预期表达式”。 我猜测它正在期待一个“表达”的case blah:并且由于某种奇怪的原因,该行不足以作为“表达式”。 因为它似乎没有注册该行,并给视图控制器一个标识符,它会产生更多的错误,主要包括“使用未声明的标识符'blahPlayerViewController';你的意思是'BlahViewController'?”。 为什么会发生? 我能做些什么来解决这个问题?


关于“ switch ”语句的一个不幸的事情是,你不能在其中声明变量。

你需要在你的switch语句之上和之外声明你的变量。

就像是:

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/84313.html

上一篇: Errors With Switch Statement to Present New View

下一篇: Variable initialization issue in switch statement