我们知道,我们可以使用if let
语句作为简写,判断可选的nil,然后展开.
但是,我想用逻辑AND运算符&&
将其与另一个表达式结合起来.
例如,我在这里进行可选的链接以展开,并可选地将我的rootViewController向下转换到tabBarController.但我不想嵌套if语句,而是想将它们组合起来.
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")
}
}
上面给出了编译错误Use of unresolved identifier 'tabBarController'
简化:
if let tabBarController = window!.rootViewController as? UITabBarController && true {
println("do stuff")
}
这将导致编译错误Bound value in a conditional binding must be of Optional type.try 了各种语法变体后,每种变体都会给出不同的编译器错误.我还没有找到顺序和括号的最佳组合.
So, the question is, is it possible and if so what is correct syntax?
Note that I want to do this with an 100 statement 103 a 101 statement or a ternary 102 operator.