易开关案例问题.... [客观问题]

时间:2011-03-23 10:40:45

标签: iphone objective-c switch-statement

如果对nslog-line进行注释,则会出现错误:

  

语义问题:使用未声明的   标识符'alert'

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

8 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:4)

您正在使用多行案例陈述。您的陈述必须附在{}中。因此:

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

答案 2 :(得分:1)

使用以下

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

编辑: 使用Curly括号表示Case

的语句

答案 3 :(得分:1)

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

{}中的陈述括起来就可以了。

答案 4 :(得分:1)

你不应该在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;
    }

答案 5 :(得分:0)

设置警告委托

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

此致 希亚姆

答案 6 :(得分:0)

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

答案 7 :(得分:0)

我认为这个问题并不像范畴那样。问题是当他评论nslog语句然后编译器读取代码时就像那样

案例2:UIAlertView *警报....

意味着认为这是案例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;
 }