您的位置:首页 > 移动开发 > IOS开发

cannot jump from switch statement to this case

2017-08-24 16:04 435 查看
用switch case出现了一个错误,cannot jump from switch statement to this case....

 查阅资料发现,是因为switch case 中是不能定义对象的,因为只要是在大括号内定义的对象

例如    switch (doBtn.tag) {

        case 107:

       

            NSLog(@"结束");

            

            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确定要结束当前的活动吗" preferredStyle:UIAlertControllerStyleAlert];

            

            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault

                                                                  handler:^(UIAlertAction * action) {

                                                                      //响应事件

                                                                     [self endActivityRequest];

                                                                  }];

            UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault

                                                                 handler:^(UIAlertAction * action) {

                                                                     //响应事件

                                                                     return ;

                                                                 }];

            [alert addAction:defaultAction];

            [alert addAction:cancelAction];  

            [self presentViewController:alert animated:YES completion:nil];

            

            break;

       

}这样就报错,只要加上一个大括号即可以

    switch (doBtn.tag) {

        case 107:

        {

            NSLog(@"结束");

            

            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确定要结束当前的活动吗" preferredStyle:UIAlertControllerStyleAlert];

            

            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault

                                                                  handler:^(UIAlertAction * action) {

                                                                      //响应事件

                                                                     [self endActivityRequest];

                                                                  }];

            UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault

                                                                 handler:^(UIAlertAction * action) {

                                                                     //响应事件

                                                                     return ;

                                                                 }];

            [alert addAction:defaultAction];

            [alert addAction:cancelAction];  

            [self presentViewController:alert animated:YES completion:nil];

            

            break;

        }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐