简单的 switch case 问题.... [目标 c]

发布于 2024-10-26 10:09:41 字数 659 浏览 3 评论 0原文

如果注释 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;
}

if comment the nslog-line, there is an error:

Semantic Issue: Use of undeclared
identifier '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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

第七度阳光i 2024-11-02 10:09:41

为了在 case 中声明新变量,您需要打开一个新作用域。要打开新范围,只需使用大括号即可,正如其他人已经编写的那样。

In order to declare a new variable inside a case you need to open a new scope. To open a new scope simply use curly braces as others have already written.

爱情眠于流年 2024-11-02 10:09:41

您正在使用多行 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;
}

You are using multiline case statement. Your statements must be enclosed in { and }. Hence:

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;
}
秋日私语 2024-11-02 10:09:41

使用以下

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 的语句。

Use the below

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

EDIT:
Use Curly brackets for the statement of Case.

狂之美人 2024-11-02 10:09:41
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;
}

将语句括在 {} 中即可达到目的。

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

Enclose the statements in {} does the trick.

荒芜了季节 2024-11-02 10:09:41

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

you should not declare variables inside switch

try this way

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

or enclose it in the braces

    case 2:
    {
    UIAlertView * alert = [[UIAlertView alloc]
                              initWithTitle: @"blabal"
                              message: @"balbalb"
                              delegate: nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        break;
    default:
        break;
    }
起风了 2024-11-02 10:09:41

设置警报代表

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

问候,
夏姆

set delegate of alert

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

Regards,
Shyam

阪姬 2024-11-02 10:09:41

我认为问题不在于范围之类的事情。问题是当他评论 nslog 语句时,编译器会读取代码,例如

case 2:UIAlertView *alert ....

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

i think the problem is not scope like thing. the problem is when he comment the nslog statement then compiler read the code some thing like that

case 2:UIAlertView *alert ....

means think this is a parameter of the case 2.
i check this the only first line after case two should not be a declaration line of variable so that means their is not scope problem

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;
 }
2024-11-02 10:09:41
////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];
  } 
////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];
  } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文