【问题标题】:OR operator inside objective C switch statment目标 C switch 语句中的 OR 运算符
【发布时间】:2012-06-15 02:31:08
【问题描述】:

我想知道在切换语句的情况下是否可以使用 (||)"or" 或 (&&)"and" 运算符。

这就是我的想法。

switch (orientation) {
            case UIDeviceOrientationPortrait:
            {
                case (UIDeviceOrientationLandscapeLeft || UIDeviceOrientationLandscapeRight):
             }
            break;
//...

如果有人可以帮助我,我将不胜感激:)

【问题讨论】:

  • AND 在 switch 中没有任何意义——你不能让orientation 同时具有两个不同的值。 OR 很容易实现,请参阅答案。

标签: iphone ios switch-statement


【解决方案1】:

&& 是不可能的,但你可以实现类似于 || 的东西通过不间断地处理多个案例:

switch (orientation)
{
   case UIDeviceOrientationPortrait:
       // ...
       break;

   case UIDeviceOrientationLandscapeLeft:
   case UIDeviceOrientationLandscapeRight:
       // ...
       break;
}

您当然也可以在案例块中使用orientation(即进一步评估它)

【讨论】:

    【解决方案2】:

    OR 隐含在switch 的结构中,缺少break

    switch (orientation) {
            case UIDeviceOrientationPortrait:
            case (UIDeviceOrientationLandscapeLeft:
            case UIDeviceOrientationLandscapeRight: {
            }
            break;
    }
    

    AND 没有意义,因为一个变量不能同时等于两个不同的整数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多