受保护开关中的外壳

发布于 2024-12-20 16:10:23 字数 2092 浏览 2 评论 0原文

可能的重复:
转换时一个使用ARC的项目“switch case is in protected scope”是什么意思?

得到以下 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最笨的告白 2024-12-27 16:10:23

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

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

顺便说一句,这已经在这里得到了回答 - 将项目转换为使用 ARC 时,“switch case 位于受保护范围内”是什么意思?

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?

孤单情人 2024-12-27 16:10:23

一般来说,您永远不应该在 case 主体内声明变量,除非您将 case 主体包装在 {} 中。大多数 C 编译器在几种情况下都会将其标记为错误(尽管通常是一个听起来非常晦涩的错误)。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文