Case in protected switch

Objective CSwitch Statement

Objective C Problem Overview


> 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

Objective C Solutions


Solution 1 - Objective C

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 - https://stackoverflow.com/questions/7562199/when-converting-a-project-to-use-arc-what-does-switch-case-is-in-protected-scop

Solution 2 - Objective C

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlazerView Question on Stackoverflow
Solution 1 - Objective CmattjgallowayView Answer on Stackoverflow
Solution 2 - Objective CHot LicksView Answer on Stackoverflow