Phil
Phil

Reputation: 461

easy switch case problem.... [objective c]

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

Upvotes: 2

Views: 1775

Answers (8)

Xiao
Xiao

Reputation: 883

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

Upvotes: 0

Iqbal Khan
Iqbal Khan

Reputation: 4617

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

Upvotes: 0

DarkDust
DarkDust

Reputation: 92306

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.

Upvotes: 7

Vaibhav Tekam
Vaibhav Tekam

Reputation: 2344

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

Upvotes: 1

GameLoading
GameLoading

Reputation: 6708

set delegate of alert

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

Regards, Shyam

Upvotes: 0

Eimantas
Eimantas

Reputation: 49335

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

Upvotes: 4

Mahesh
Mahesh

Reputation: 34605

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.

Upvotes: 1

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

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.

Upvotes: 1

Related Questions