switch 语句在 Objective-C 中如何工作?
我在 Objective-C 中使用 switch 语句,当我将一个 switch 语句嵌套在另一个 switch 语句中时,编译器发现了很多错误。所以我的一般问题是 switch 语句如何影响我可以调用的方法,以及什么类型的行为导致编译器生成错误?
switch ([indexPath section])
{
case 0:
cell.textLabel.text = [infoKeysArray objectAtIndex:indexPath.row];
CountdownFormatter *formatter = [[CountdownFormatter alloc] init];
switch (indexPath.row) {
case 0:
cell.detailTextLabel.text = [formatter stringForCountdownInterval:taskInfo.duration];
break;
case 1:
cell.detailTextLabel.text = [formatter stringForCountdownInterval:taskInfo.elapsedTime];
default:
break;
}
[formatter release];
break;
case 1:
cell.textLabel.text = [actionsKeysArray objectAtIndex:indexPath.row];
//NSArray *segments = [NSArray arrayWithObjects:@"Low", @"Medium", @"High", nil];
switch (indexPath.row) {
case 0:
cell.detailTextLabel.text = priority;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
break;
case 1:
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
UISwitch *switch = [[UISwitch alloc] init];
break;
case 2:
break;
default:
break;
}
break;
case 2:
cell.textLabel.text = [detailsKeysArray objectAtIndex:indexPath.row];
break;
default:
break;
}
这是我遇到问题的地方。为什么我不能在此嵌套 switch 语句中声明 UISwitch?我知道我写了 [[UISwitch alloc] init] 但编译器将其标记为错误。有什么原因会发生这种情况吗?
I am using switch statements in Objective-C, and when I nest a switch statement inside another switch statement, a lot of errors are being found by the compiler. So my general question is how do switch statements affect what methods I can call, and what type of behavior is causing the compiler to generate errors?
switch ([indexPath section])
{
case 0:
cell.textLabel.text = [infoKeysArray objectAtIndex:indexPath.row];
CountdownFormatter *formatter = [[CountdownFormatter alloc] init];
switch (indexPath.row) {
case 0:
cell.detailTextLabel.text = [formatter stringForCountdownInterval:taskInfo.duration];
break;
case 1:
cell.detailTextLabel.text = [formatter stringForCountdownInterval:taskInfo.elapsedTime];
default:
break;
}
[formatter release];
break;
case 1:
cell.textLabel.text = [actionsKeysArray objectAtIndex:indexPath.row];
//NSArray *segments = [NSArray arrayWithObjects:@"Low", @"Medium", @"High", nil];
switch (indexPath.row) {
case 0:
cell.detailTextLabel.text = priority;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
break;
case 1:
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
UISwitch *switch = [[UISwitch alloc] init];
break;
case 2:
break;
default:
break;
}
break;
case 2:
cell.textLabel.text = [detailsKeysArray objectAtIndex:indexPath.row];
break;
default:
break;
}
Here is where I am having issues. Why can I not declare a UISwitch inside this nested switch statement? I know I wrote [[UISwitch alloc] init] but the compiler is flagging it as an error. Any reason that would happen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不能说比这个答案更好了为什么不能在 switch 语句中声明变量?。
问题在于您在何处/何时声明变量以及它们所在的范围。
I can't say it any better than this answer Why can't variables be declared in a switch statement?.
The issue is where/when you are declaring variables and the scope they are in.
Switch 语句对 obj-c 没有任何影响。它们是纯 C 构造,在 Obj-C 中的工作方式与在 C 中的工作方式完全相同。至于错误,如果在嵌套 switch 语句时遇到编译器错误,则可能会出现某种语法错误。
Switch statements have no effect whatsoever on obj-c. They're pure C constructs, and work in Obj-C exactly as they do in C. As for errors, you likely have some sort of syntactic error if you're getting compiler errors when nesting switch statements.
对我来说工作正常 :
它们不会。
程序员的行为。
编辑 - 为了响应您发布的代码,在无法编译或查看错误是什么的情况下,我怀疑:
可能是:
请注意大括号以及将变量命名为关键字以外的名称的选择。
Works fine for me:
They don't.
The programmer's behaviour.
EDIT - In response to the code you posted, without being able to compile or see what the error is I would suspect:
could be:
Note the braces and the choice of naming the variable something other than a keyword.