1

In my Xcode project, I'm trying to show an alert after the mail UI is closed only if the mail was cancelled but I'm getting an error (Switch Case is in protected scope) and it is showing on all the lines that start with "case".

typedef enum MFMailComposeResult MFMailComposeResult;
switch (result)
{
    case MFMailComposeResultCancelled:
            [self dismissViewControllerAnimated:YES completion:NULL];
        UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:@"Report Sent"
                                                           message:@"Your report has been sent and it will be reviewed. If you are in an emergency, remember to call 911."
                                                          delegate:self
                                                 cancelButtonTitle:@"OK"
                                                 otherButtonTitles:nil];
        [theAlert show];

        NSLog(@"Mail cancelled");
        break;
    case MFMailComposeResultSaved:
        NSLog(@"Mail saved");
        break;
    case MFMailComposeResultSent:

        NSLog(@"Mail sent");
        break;
    case MFMailComposeResultFailed:
        NSLog(@"Mail sent failure: %@", [error localizedDescription]);
        break;
    default:
        break;
}
  • Please quote the exact error message, and indicate which line it occurred on. – Hot Licks Aug 19 '14 at 03:23
  • 2
    (But try putting `{}` around the first `case` body -- you can't have "bare" declarations inside a `switch`.) – Hot Licks Aug 19 '14 at 03:24
  • @HotLicks Thanks, that fixed it. Such a stupid mistake! –  Aug 19 '14 at 03:29
  • @stevetheipad remember that Google (or even SO search) is your friend. Searching your exact question title gave the answer you were looking for on the first hit. – Stonz2 Aug 19 '14 at 15:30

2 Answers2

4

Put brackets around multilined cases

switch (result)
{
    case MFMailComposeResultCancelled: {
        [self dismissViewControllerAnimated:YES completion:NULL];
        UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:@"Report Sent"
                                                           message:@"Your report has been sent and it will be reviewed. If you are in an emergency, remember to call 911."
                                                          delegate:self
                                                 cancelButtonTitle:@"OK"
                                                 otherButtonTitles:nil];
        [theAlert show];

        NSLog(@"Mail cancelled");
    }
        break;
    case MFMailComposeResultSaved:
        NSLog(@"Mail saved");
        break;
    case MFMailComposeResultSent:

        NSLog(@"Mail sent");
        break;
    case MFMailComposeResultFailed:
        break;
    default:
        break;
}
chrs
  • 5,906
  • 10
  • 43
  • 74
  • 1
    No. This is not like an if-statement, and the number of lines is not relevant. – Eiko Aug 19 '14 at 07:59
  • Why do you need the {} then? – chrs Aug 19 '14 at 13:14
  • 1
    You need the brackets to define the scope of a created object so that ARC knows when to deallocate the memory. Without the brackets, the compiler wouldn't know the proper lifetime of the object and could result in either a memory leak or premature-deallocation/bad access. – Stonz2 Aug 19 '14 at 15:27
1

try this

typedef enum MFMailComposeResult MFMailComposeResult;
    switch (result)
    {
        case MFMailComposeResultCancelled:
    {
                [self dismissViewControllerAnimated:YES completion:NULL];
            UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:@"Report Sent"
                                                               message:@"Your report has been sent and it will be reviewed. If you are in an emergency, remember to call 911."
                                                              delegate:self
                                                     cancelButtonTitle:@"OK"
                                                     otherButtonTitles:nil];
            [theAlert show];

            NSLog(@"Mail cancelled");
            break;
    }
        case MFMailComposeResultSaved:
    {
            NSLog(@"Mail saved");
            break;
    }
        case MFMailComposeResultSent:
    {

            NSLog(@"Mail sent");
            break;
    }
        case MFMailComposeResultFailed:
    {
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            break;
    }
        default:
            break;
    }
Sport
  • 8,570
  • 6
  • 46
  • 65