PeterK
PeterK

Reputation: 4301

Why the difference in UIAlertView?

I have the following code, which works:

// Load new game screen
-(IBAction)newGame_button:(id)sender {
    myAlert = [[UIAlertView alloc]
                    initWithTitle:@"Varning" 
                    message:@"Om du går vidare kommer pågående spel stoppas och nollställas!"
                    delegate:self
                    cancelButtonTitle:@"Tillbaka"
                    otherButtonTitles:@"Fortsätt", nil];
    myAlert.tag=kTagNewGame;
    [myAlert show];
    [myAlert release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    switch(myAlert.tag ) {
        case kTagContinueGame:
            NSLog(@"kTagContinueGame");
            NSMutableArray *continueGameArray = [[NSMutableArray alloc] initWithCapacity:0];

            AccessCurrentGameData *getCurrentGameInfo = [AccessCurrentGameData new];
            continueGameArray = [getCurrentGameInfo continueTheCurrentGame];
            [getCurrentGameInfo release];
            [continueGameArray retain];
            [continueGameArray release];

            QandA_ViewController * temp = [[QandA_ViewController alloc] init];
            [self setQanda_ViewController:temp];
            [temp release];
            [[self navigationController] pushViewController:qanda_ViewController animated:YES];
            break;
        case kTagNewGame:
            NSLog(@"kTagNewGame");
            AccessCurrentGameData *zeroCurrentGameFileFunction = [AccessCurrentGameData new];
            [zeroCurrentGameFileFunction firstCreationOrRestoreOfGameDataFile];
            [zeroCurrentGameFileFunction release];

            NewGameViewController * temp2 = [[NewGameViewController alloc] init];
            [self setNewGameViewController:temp2];
            [temp2 release];
            [[self navigationController] pushViewController:newGameViewController animated:YES];
            break;
        default:
            break;
    }
}

However, when I remove the NSLog's I get the following error:

Expected expression before 'NSMutableArray'

I do not understand why this is happening. I can probably leave the NSLog's but why?

Upvotes: 3

Views: 504

Answers (1)

Vladimir
Vladimir

Reputation: 170839

The problem is that variable declaration cannot directly follow case label. To solve that I usually put the whole case block in {}:

...
case kTagNewGame:
{
        AccessCurrentGameData *zeroCurrentGameFileFunction = [AccessCurrentGameData new];
        [zeroCurrentGameFileFunction firstCreationOrRestoreOfGameDataFile];
        [zeroCurrentGameFileFunction release];

        NewGameViewController * temp2 = [[NewGameViewController alloc] init];
        [self setNewGameViewController:temp2];
        [temp2 release];
        [[self navigationController] pushViewController:newGameViewController animated:YES];
        break;
}
...

Upvotes: 5

Related Questions