We know that we can use the if let as a shorthand to check the optional nil, and then unzip it.
However, I want to combine this with another expression using the logical AND operator & && .
So, for example, here I am doing an optional chain to expand and maybe lower my rootViewController to tabBarController. But instead of having nested if statements, I would like to combine them.
if let tabBarController = window!.rootViewController as? UITabBarController { if tabBarController.viewControllers.count > 0 { println("do stuff") } }
Combined value:
if let tabBarController = window!.rootViewController as? UITabBarController && tabBarController.viewControllers.count > 0 { println("do stuff") } }
Above compilation error Using unresolved identifier 'tabBarController'
Simplifying:
if let tabBarController = window!.rootViewController as? UITabBarController && true { println("do stuff") }
This gives a compilation error. The bound value in the conditional binding must be of an optional type . Having made various syntactic changes, each of them gives a different compiler error. I have yet to find a winning combination of order and parentheses.
So the question is, is it possible, and if so, what is the correct syntax?
Note that I want to do this with an if not a switch or a ternary operator ? .
expression swift
Max MacLeod Aug 08 '14 at 11:44 2014-08-08 11:44
source share