【问题标题】:Case in protected switch [duplicate]保护开关中的外壳 [重复]
【发布时间】:2011-12-13 00:01:12
【问题描述】:

可能重复:
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

但是一个错误:

【问题讨论】:

    标签: objective-c switch-statement


    【解决方案1】:

    您应该用{} 大括号将每个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?

    【讨论】:

    • 没有必要将每个开关都包装在{} 中,只需要那些声明变量(显式或通过宏或编译器调配)的开关。 ARC 与它的关系相对较小。
    【解决方案2】:

    一般来说,你不应该在 case 正文中声明变量,除非你将案例正文包裹在 {} 中。大多数 C 编译器会在几种情况下将其标记为错误(尽管通常是听起来非常晦涩的错误)。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-30
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多