【问题标题】:IOS expected expression errorIOS预期表达式错误
【发布时间】:2016-11-21 06:44:37
【问题描述】:

我想在 switch 中使用 if 语句,但我不断收到预期的表达式错误。如果有人能帮我解决这个问题,我将不胜感激。

switch ([ApplicationModel getApplicationType]) {
        case CSApplication: {}
            break;
        case RAApplication: 
        {
            if (indexPath.row == FloorLevelIndex || indexPath.row == RoomIndex) 
            {
                [cell setUserInteractionEnabled: YES];            
            }
        }
            break;
        default:
            break;
    }

【问题讨论】:

  • 您不需要在案例开头设置大括号。
  • 哪一行出现错误?
  • @cjrieck if (indexPath.row == FloorLevelIndex || indexPath.row == RoomIndex) 这个
  • @AnastasiaY 和 FloorLevelIndexRoomIndex 是什么?只是NSInteger 或类似的东西?如果这些是类型,您将无法将数字与类型进行比较。你可能想要这些的数字表示。
  • @cjrieck 他们刚刚被定义,它没有工作。您应该将此添加为答案

标签: ios objective-c if-statement switch-statement


【解决方案1】:

如果 FloorLevelIndexRoomIndex 是类型,您将无法将数字与类型进行比较(因此会出现错误)。您可能想要这些数字表示或使用NSInteger 值定义它们

【讨论】:

    【解决方案2】:

    确保 [ApplicationModel getApplicationType] 返回 swtich case 支持的有效数据类型。

    switch语句中使用的变量只能是整数, 可转换的整数(byte、short、char)、字符串和枚举

    应该是这样的:

    switch ([ApplicationModel getApplicationType]) {
            case CSApplication: 
                 break;
    
           case RAApplication: 
                if (indexPath.row == FloorLevelIndex || indexPath.row == RoomIndex) 
                {
                    [cell setUserInteractionEnabled: YES];            
                }
                break;
    
           default:
                break;
        }
    

    【讨论】:

    • 我确信它返回了有效数据。问题在于定义的 FloorLevelIndex 和 RoomIndex
    【解决方案3】:

    我刚刚在我的 Xcode 中尝试了这段代码,它运行良好:

    NSLayoutAttribute attr = NSLayoutAttributeLeft;
    switch (attr) {
        case NSLayoutAttributeTop: {}
            break;
        case NSLayoutAttributeLeft: {
            if (YES) {
                NSLog(@"works");
            }
        }
            break;
        default:
            break;
    }
    

    所以你的 switch 语句的结构是正确的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多