使用Swift,如果使用逻辑与运算符&& [英] Using the Swift if let with logical AND operator &&

查看:135
本文介绍了使用Swift,如果使用逻辑与运算符&&的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道我们可以使用,如果让语句作为缩写,检查可选的零,然后解开。

We know that we can use an if let statement as a shorthand to check for an optional nil then unwrap.

但是,我想使用逻辑AND运算符&& 与另一个表达式组合。

However, I want to combine that with another expression using the logical AND operator &&.

所以,例如,这里我可以选择链接来解开和可选地将我的rootViewController下载到tabBarController。但是,而不是嵌套if语句,我想结合它们。

So, for example, here I do optional chaining to unwrap and optionally downcast my rootViewController to tabBarController. But rather than have nested if statements, I'd like to combine them.

if let tabBarController = window!.rootViewController as? UITabBarController {
    if tabBarController.viewControllers.count > 0 {
        println("do stuff")
     }
 }

组合赋值:

if let tabBarController = window!.rootViewController as? UITabBarController &&
    tabBarController.viewControllers.count > 0 {
        println("do stuff")
     }
}

上面给出编译错误使用未解析的标识符'tabBarController'

简化:

if let tabBarController = window!.rootViewController as? UITabBarController && true {
   println("do stuff")
}

编译错误条件绑定中的绑定值必须为可选类型。尝试各种语法变体后,每个都会产生不同的编译器错误。我还没有找到顺序和括号的获胜组合。

This gives a compilation error Bound value in a conditional binding must be of Optional type. Having attempted various syntactic variations, each gives a different compiler error. I've yet to find the winning combination of order and parentheses.

所以,问题是,是否可能,如果是,什么是正确的语法? / strong>

So, the question is, is it possible and if so what is correct syntax?

请注意,如果语句不是 a 开关语句或三元运算符。

Note that I want to do this with an if statement not a switch statement or a ternary ? operator.

推荐答案

从Swift 1.2起,这个 现在可以 Swift 1.2和Xcode 6.3测试版发行说明指出:

As of Swift 1.2, this is now possible. The Swift 1.2 and Xcode 6.3 beta release notes state:


更强大的可选展开如果let - 如果let构造
现在可以一次解开多个可选项,以及包含
介入布尔条件。这样就可以在没有不必要的嵌套的情况下表达条件
控制流。

More powerful optional unwrapping with if let — The if let construct can now unwrap multiple optionals at once, as well as include intervening boolean conditions. This lets you express conditional control flow without unnecessary nesting.

使用上述语句,语法将是: / p>

With the statement above, the syntax would then be:

if let tabBarController = window!.rootViewController as? UITabBarController where tabBarController.viewControllers.count > 0 {
        println("do stuff")
}

其中子句。

另一个例子,这次将 AnyObject 转换为 Int ,展开可选项,并检查展开的可选项符合条件:

Another example, this time casting AnyObject to Int, unwrapping the optional, and checking that the unwrapped optional meets the condition:

if let w = width as? Int where w < 500
{
    println("success!")
}

对于现在使用Swift 3的用户,where已被逗号替换。因此,相当于:

For those now using Swift 3, "where" has been replaced by a comma. The equivalent would therefore be:

if let w = width as? Int, w < 500
{
    println("success!")
}

这篇关于使用Swift,如果使用逻辑与运算符&amp;&amp;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆