Using Swift if let with the logical operator AND &&

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 ? .

+62
expression swift
Aug 08 '14 at 11:44
source share
5 answers

With Swift 1.2, this one is now possible . Swift 1.2 and Xcode 6.3 beta release :

A more powerful deployment option with if - The if let construct now you can deploy multiple options at once, as well as enable intermediate Boolean conditions. This allows you to express a conditional control flow without unnecessary investment.

With the above statement, the syntax will be as follows:

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

In this case, the where clause is used.

Another example: this time discarding AnyObject in Int , AnyObject optional, and checking that the expanded optional parameter matches the condition:

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

For those who are currently using Swift 3, "where" is replaced by a comma. So the equivalent is:

 if let w = width as? Int, w < 500 { println("success!") } 
+91
Feb 24 '15 at 11:32
source share

In Swift 3, a Max MacLeod example would look like this:

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

where been replaced by ,

+43
Sep 21 '16 at 13:53 on
source share

The maximum answer is correct and one way to do this. Note that this is written as follows:

if let a = someOptional where someBool { }

First, someOptional expression will be resolved. If this fails, the someBool expression someBool not be evaluated (short circuit evaluation, as you would expect).

If you want to write this, it can be done like this:

if someBool, let a = someOptional { }

In this case, someBool is evaluated someBool , and only if it evaluates to true does the someOptional expression someOptional .

+30
Nov 17 '15 at 21:42
source share

It's impossible.

From Swift grammar

GRAMMATIC STATEMENT

if-statement → if if-condition code-block else-clauseopt

if-condition → expression | declaration

else-clause → else code block | else if-statement

The value of any condition in the if statement must be of a type that conforms to the BooleanType protocol. The condition may also be an optional binding declaration, as described in the "Optional binding" section.

if-condition must be an expression or declaration. You cannot have expressions and declarations.

let foo = bar is a declaration; it does not evaluate the value corresponding to BooleanType . It declares a constant / variable foo .

Your original solution is good enough, it is more readable, and then combines the conditions.

+5
Aug 09 '14 at 1:14
source share

I think your original proposal is not so bad. An alternative (messier) would be:

 if ((window!.rootViewController as? UITabBarController)?.viewControllers.count ?? 0) > 0 { println("do stuff") } 
0
Aug 09 '14 at 1:11
source share



All Articles