在iOS中的switch错误

30 浏览
0 Comments

在iOS中的switch错误

我试着使用switch来执行不同的功能

但是在case 10:下面的那行代码会报错expected expression,我不明白为什么,你能帮我解决吗?

   UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"太棒了!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertView show];

这些代码报错了。

0
0 Comments

在iOS中出现switch错误的原因是在switch语句中直接初始化UIAlertview。正确的做法是将它放在花括号中。解决方法是将初始化UIAlertview的代码放在花括号中。

代码示例:

switch (val) {
    case 10:
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"great!!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertView show];
    }
        break;
}

请将NSLog放在UIALertView上方,你就会发现错误消失了。

0