iOS:switch case中报错expected expression

switch语句在java中使用很正常,但在OC里就出现问题了:

像下面的语句XZXHelpViewController将无法识别,从而报expected expression的错误

如果之前加上一条NSLog语句或者其他一些语句,则可以识别,但default:break;语句将报错

- (void)buttonClicked:(UIButton *)sender{

    switch (sender.tag) {

        case 1:

    // NSLog(@"moonglow");

            XZXHelpViewController *help = [[XZXHelpViewControlleralloc] initWithNibName:@"XZXHelpViewController"bundle:nil];

            [selfpresentViewController:help animated:YEScompletion:NULL];

            break;

        default:

            break;

    }

解决的办法是在case语句后加大括号

- (void)buttonClicked:(UIButton *)sender{

    switch (sender.tag) {

        case 1:{

            XZXHelpViewController *help = [[XZXHelpViewControlleralloc] initWithNibName:@"XZXHelpViewController"bundle:nil];

            [selfpresentViewController:help animated:YEScompletion:NULL];

            break;

   } 

        default:

            break;

    }

有人说加分号也可以,其实跟加NSLog一个意思,会导致default:break;报错。

至于为什么需要加大括号,如果有大牛看到此帖,还请指教!

posted @ 2013-05-03 18:10  Moonglow  阅读(2413)  评论(0编辑  收藏  举报