【问题标题】:How to paste [array count] to case:如何将 [数组计数] 粘贴到大小写:
【发布时间】:2012-07-25 15:37:50
【问题描述】:

我有开关。其中一种情况必须是数组的计数:

    int count = [array count];
    switch (someValue) {
        case 0:
            [self foo];
            break;
        case count:
            [self bar];
            break;

        default:
            break;
    }

但是编译器告诉:

Expression is not an integer constant expression

如何从 [array count] 生成 const int?

【问题讨论】:

  • 不可能。 Switch 不能那样工作,你必须创建自己的 switch 式控制结构(如果你愿意,我可以这样做)。

标签: objective-c ios arrays switch-statement


【解决方案1】:

正如错误所暗示的,案例必须都是常数。你需要一个if 语句来检查动态大小写:

    int count = [array count];
    switch (someValue) {
        case 0:
            [self foo];
            break;
        default:
            if (someValue == count)
                [self bar];
            break;
    }

【讨论】:

    【解决方案2】:
    if(some value == 0) {
        [self foo];
    } else if (someValue == [array count]) {
        [self bar]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-23
      • 1970-01-01
      • 1970-01-01
      • 2021-07-22
      相关资源
      最近更新 更多