【问题标题】:easy switch case problem.... [objective c]简单的 switch case 问题.... [目标 c]
【发布时间】:2011-03-23 10:40:44
【问题描述】:

如果注释nslog-line,就会出错:

语义问题:使用未声明 标识符“警报”

switch ([[array objectAtIndex:0]intValue]) {
    case 2:
        NSLog(@"Allergie alarm");   << commenting this, gives me an error!!!
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle: @"blabal"
                              message: @"balbalb"
                              delegate: nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        break;
    default:
        break;
}

【问题讨论】:

    标签: iphone objective-c switch-statement


    【解决方案1】:

    为了在case 中声明一个新变量,您需要打开一个新范围。要打开一个新范围,只需使用其他人已经写过的花括号。

    【讨论】:

      【解决方案2】:

      您正在使用多行 case 语句。您的陈述必须包含在{} 中。因此:

      case 2: {
          NSLog(@"Allergie alarm");
          UIAlertView *alert = [[UIAlertView alloc]
                                initWithTitle: @"blabal"
                                message: @"balbalb"
                                delegate: nil
                                cancelButtonTitle:@"OK"
                                otherButtonTitles:nil, nil];
          [alert show];
          [alert release];
          break;
      }
      

      【讨论】:

        【解决方案3】:

        使用下面的

        switch ([[array objectAtIndex:0]intValue]) {
            case 2:
               {
                   NSLog(@"Allergie alarm");   << commenting this, gives me an error!!!
                   UIAlertView *alert = [[UIAlertView alloc]
                                      initWithTitle: @"blabal"
                                      message: @"balbalb"
                                      delegate: nil
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil, nil];
                   [alert show];
                   [alert release];
                }
                break;
            default:
                break;
        }
        

        编辑: Case 的语句使用大括号。

        【讨论】:

          【解决方案4】:
          case 2:
          {
              NSLog(@"Allergie alarm");   << commenting this, gives me an error!!!
              UIAlertView *alert = [[UIAlertView alloc]
                                    initWithTitle: @"blabal"
                                    message: @"balbalb"
                                    delegate: nil
                                    cancelButtonTitle:@"OK"
                                    otherButtonTitles:nil, nil];
              [alert show];
              [alert release];
              break;
          }
          

          将语句括在{} 中就可以了。

          【讨论】:

            【解决方案5】:

            你不应该在 switch 中声明变量

            试试这个方法

            UIAlertView *alert;
            switch ([[array objectAtIndex:0]intValue]) {
                case 2:
            
                alert = [[UIAlertView alloc]
                                          initWithTitle: @"blabal"
                                          message: @"balbalb"
                                          delegate: nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil, nil];
                    [alert show];
                    [alert release];
                    break;
                default:
                    break;
            }
            

            或者用大括号括起来

                case 2:
                {
                UIAlertView * alert = [[UIAlertView alloc]
                                          initWithTitle: @"blabal"
                                          message: @"balbalb"
                                          delegate: nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil, nil];
                    [alert show];
                    [alert release];
                    break;
                default:
                    break;
                }
            

            【讨论】:

              【解决方案6】:

              设置警报委托

               UIAlertView *alert = [[UIAlertView alloc]
                                            initWithTitle: @"blabal"
                                            message: @"balbalb"
                                            delegate: self
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil, nil];
              

              问候, 夏姆

              【讨论】:

                【解决方案7】:

                我认为问题不在于范围之类的东西。问题是当他评论 nslog 语句时,编译器会读取类似这样的代码

                案例2:UIAlertView *alert ....

                表示认为这是案例 2 的参数。 我检查了案例二之后唯一的第一行不应该是变量的声明行,这意味着它们不是范围问题

                switch (2) {
                     case 2:
                         ;
                        //NSLog(@"Allergie alarm");  // << commenting this, gives me an error!!!
                         UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"blabal" message: @"balbalb" delegate: nil  cancelButtonTitle:@"OK" otherButtonTitles:nil];
                         [alert show];
                         [alert release];
                         break;
                
                     default:
                         break;
                 }
                

                【讨论】:

                  【解决方案8】:
                  ////some code
                  switch ([[array objectAtIndex:0]intValue]) {
                    case 2:
                      NSLog(@"Allergie alarm");   << commenting this, gives me an error!!!
                      [self showAlert];
                      break;
                  default:
                      break;
                  }
                  ////some code
                  
                  
                  
                  
                  - (void) showAlert{
                      UIAlertView *alert = [[UIAlertView alloc]
                                            initWithTitle: @"blabal"
                                            message: @"balbalb"
                                            delegate: nil
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil, nil];
                      [alert show];
                      [alert release];
                    } 
                  

                  【讨论】:

                  • 添加一个方法和另一个方法调用来解决这个简单的问题并不是一个好的解决方案,尽管它确实解决了问题。
                  猜你喜欢
                  • 2022-06-29
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2016-06-12
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2020-06-27
                  相关资源
                  最近更新 更多