保护开关的情况

时间:2011-12-13 00:01:13

标签: objective-c switch-statement

  

可能重复:
  When converting a project to use ARC what does “switch case is in protected scope” mean?

获得以下xcode: 但是,当我尝试在案例1(或空)中放置一些东西时,它给了我一个错误?

奇怪的问题,因为我不知道受保护的交换机是什么以及我应该如何解决它。有没有人有解决方案或线索来解决这个问题?怪异..

- (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

但是错误:

Error

2 个答案:

答案 0 :(得分:201)

您应该使用{}大括号包装每个switch语句。例如:

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

这已经在这里得到了解答 - When converting a project to use ARC what does "switch case is in protected scope" mean?

答案 1 :(得分:34)

一般情况下,除非将案例正文包装在case中,否则不应在{}正文中声明变量。大多数C编译器会在几种情况下将其标记为错误(尽管通常是一个非常模糊的错误)。

原因是编译器无法判断变量的范围在何处结束,如果在第一个case主体中有声明,那么它看起来像第二个case是分支到变量范围的中间,使编译器想知道应该如何/是否应该初始化。