受保护开关中的外壳
可能的重复:
转换时一个使用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
但是一个错误:
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该用
{}
大括号包裹每个 switch 语句。例如:顺便说一句,这已经在这里得到了回答 - 将项目转换为使用 ARC 时,“switch case 位于受保护范围内”是什么意思?
You should wrap each switch statement with
{}
braces. For example: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?
一般来说,您永远不应该在
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 secondcase
is a branch into the middle of the variable's scope, making the compiler wonder how/if it should be initialized.