82

Possible Duplicate:
When converting a project to use ARC what does “switch case is in protected scope” mean?

Got the following xcode: But when i try to put something in case 1 (or empty) it's giving me an error?

Weird problem because i dont know what a protected switch is and how i should fix it. Does anyone has a solution or clue to fix this? Weird..

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *controller;

    switch(indexPath.row) {
        case 0:
            NSLog(@"0");

            //create instance of EKEventStore
            EKEventStore *eventStore = [[EKEventStore alloc] init];

            //creating instance of EKEvent
            EKEvent *event  = [EKEvent eventWithEventStore:eventStore];

            //setting the appropriate properties of the new event
            event.title     = @"Woow";

            //event.startDate = [[NSDate alloc] init];



            NSDateComponents *myDate2 = [[NSDateComponents alloc] init];
            [myDate2 setDay:13];
            [myDate2 setMonth:12];
            [myDate2 setYear:2011];
            [myDate2 setHour:00];
            [myDate2 setMinute:34];

            event.startDate = [[NSCalendar currentCalendar] dateFromComponents:myDate2];

            event.endDate   = [[NSDate alloc] initWithTimeInterval:3600 sinceDate:event.startDate];
            event.location = @"game2";
            event.notes = @" game";

            event.alarms = [NSArray arrayWithObject:[EKAlarm alarmWithAbsoluteDate:event.startDate]];

            [event setCalendar:[eventStore defaultCalendarForNewEvents]];
            NSError *error;
            [eventStore saveEvent:event span:EKSpanThisEvent error:&error];

            break;

        case 1:
            NSLog(@"1");    






            break;






    }

    {



        self.EKController.title = [self.EKList objectAtIndex:[indexPath row]];






    }

}


@end

But an error:

Error

Community
  • 1
  • 1
Blazer
  • 14,259
  • 3
  • 30
  • 53

2 Answers2

201

You should wrap each switch statement with {} braces. For example:

switch (someInt) {
    case 0:
    {
        NSLog(@"Case 0");
    }
    break;
    case 1:
    {
        NSLog(@"Case 1");
    }
    break;
}

This has been answered already here by the way - When converting a project to use ARC what does "switch case is in protected scope" mean?

Community
  • 1
  • 1
mattjgalloway
  • 34,792
  • 12
  • 100
  • 110
  • 2
    It is not necessary to wrap EVERY switch in`{}`, only those that declare variables (explicitly or via a macro or compiler swizzle). ARC has relatively little to do with it. – Hot Licks Jun 07 '14 at 11:27
34

In general, you should never declare variables inside a case body, unless you wrap the case body in {}. Most C compilers will flag that as an error under several circumstances (though often a very obscure-sounding error).

The reason for this is that the compiler can't tell where the scope of the variable ends, and if you have a declaration in the first case body then it looks like the second case is a branch into the middle of the variable's scope, making the compiler wonder how/if it should be initialized.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151