switch 语句在 Objective-C 中如何工作?

发布于 2024-12-29 04:11:16 字数 1694 浏览 3 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(3

踏月而来 2025-01-05 04:11:16

我不能说比这个答案更好了为什么不能在 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.

我三岁 2025-01-05 04:11:16

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.

叹倦 2025-01-05 04:11:16

当我将一个 switch 语句嵌套在另一个 switch 语句中时,编译器发现了很多错误

对我来说工作正常 :

int main()
{
    int x = 1;
    int y = 2;

    switch(x)
    {
    case 1:
       switch(y)
       {
       case 2:
         printf("Hello!\n");
       }
    }

    return 0;
}

所以我的一般问题是 switch 语句如何影响我可以调用的方法

它们不会。

什么类型的行为导致编译器生成错误?

程序员的行为。

编辑 - 为了响应您发布的代码,在无法编译或查看错误是什么的情况下,我怀疑:

case 1:
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        UISwitch *switch = [[UISwitch alloc] init];
        break;

可能是:

case 1:
{
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        UISwitch *myswitch = [[UISwitch alloc] init];
        break;
}

请注意大括号以及将变量命名为关键字以外的名称的选择。

when I nest a switch statement inside another switch statement, a lot of errors are being found by the compiler

Works fine for me:

int main()
{
    int x = 1;
    int y = 2;

    switch(x)
    {
    case 1:
       switch(y)
       {
       case 2:
         printf("Hello!\n");
       }
    }

    return 0;
}

So my general question is how do switch statements affect what methods I can call

They don't.

and what type of behavior is causing the compiler to generate errors?

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:

case 1:
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        UISwitch *switch = [[UISwitch alloc] init];
        break;

could be:

case 1:
{
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        UISwitch *myswitch = [[UISwitch alloc] init];
        break;
}

Note the braces and the choice of naming the variable something other than a keyword.

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