views:

97

answers:

2

I'm getting an error in my switch statement with some multi-line Objective-c code:

- (void)mailComposeController:(MFMailComposeViewController*)controller
          didFinishWithResult:(MFMailComposeResult)result
                        error:(NSError*)error 
{   
    // Notifies users about errors associated with the interface
    switch (result)
    {
        case MFMailComposeResultCancelled:
            break;
        case MFMailComposeResultFailed:
//              NSLog(@"Mail Failed");
            UIAlertView *alert = [[UIAlertView alloc] 
                                initWithTitle:NSLocalizedString(@"Error", @"Error")
                                message:[error localizedDescription]
                                delegate:nil
                                cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
                                otherButtonTitles:nil];
            [alert show];
            [alert release];
            break;
        default:
            break;
    }
}

If I uncomment the line with the NSLog, it works fine. What's causing this error? Is there any way to use this kind of formatting?

+12  A: 
KennyTM
Thanks for the answer. Nothing to do with the formatting then, but the declaration.
nevan
+8  A: 

The issues is with how switch is defined. You can't have a variable declaration on the line following the case. You can fix it by wrapping the entire case in a new scope

    case MFMailComposeResultFailed:
    {
//              NSLog(@"Mail Failed");
        UIAlertView *alert = [[UIAlertView alloc] 
                            initWithTitle:NSLocalizedString(@"Error", @"Error")
                            message:[error localizedDescription]
                            delegate:nil
                            cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
                            otherButtonTitles:nil];
        [alert show];
        [alert release];
        break;
    }
Joshua Weinberg