Using the Swift If Let with Logical and Operator &&

Using the Swift if let with logical AND operator &&

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

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.

With the statement above, the syntax would then be:

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

This uses the where clause.

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!")
}

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!")
}

Using if let with logical or operator

Try to use ?? operator:

if let amount = datasource?.incrementForCount?(count) ?? datasource?.fixedIncrement 
{
count += amount
}

is it possible to combine the 2 if let statements into a single ORed one ?

It is possible to have several let statements like

if let a = optionalA, b = optionalB { ... }

And all of them should return non-nil value to pass.

But if you also want to use a logical condition it:

  1. Could be only the one
  2. Should be placed on the first place, before any let statements

Swift: Combine condition and if-let with logical or

The equivalent to your code example would be:

if text == "" || Int(text) ?? 2 < 2 {
print("valid")
// do your previous "something
} else {
print("invalid")
}

which yields

"" -> valid

"1" -> valid

"2" -> invalid

"abc" -> invalid

Multiple if let and OR condition in swift

if let value1 = profile.value1, value1 != “” {}
else if value2 = profile.value2, value2 != “” {}
else if value3 = profile.value3, value3 != “” {}

here , acts like &&

Pay attention:
For Swift 2 replace , with where

While loop with double conditional and || logical operator in Swift

You just need to break loop

while S < 10  {
S += 1
let D = Int.random(in: 0...24)
if D == 0 {
print("D = 0 in a cycle \(S)/10")
break
}
}

Are && and , the same in Swift?

They can be used in similar situations but that does not mean they are exactly the same.

Consider:

if (a && b) || c

you cannot write

if (a, b) || c

even a && b || c is different from a, b || c.

if a, b is something like

if a {
if b {
...
}
}

Both expressions have to be evaluated to true but they are still two separate expressions. We shouldn't imagine && there.

Why do we need the , operator?

The operator is needed to combine optional binding with boolean conditions, e.g.

if let a = a, a.isValid() {

becuase && wouldn't help us in such situations.

swift multiple conditions if statement with && operation

you can simply use isEmpty property.

if !self.textFieldOne.text!.isEmpty && !self.textFieldTwo.text!.isEmpty && !self.textFieldThree.text!.isEmpty && !self.textFieldFour.text!.isEmpty {
...

}

or you can also safely unwrap the text value and the check that it is empty of not

if let text1 = self.textFieldOne.text, text2 = self.textFieldTwo.text, text3 = self.textFieldthree.text,text4 = self.textFieldFour.text where !text1.isEmpty && !text2.isEmpty && !text3.isEmpty && !text4.isEmpty {
...
}

Or you can compare with Empty "" String

if self.textFieldOne.text != "" && self.textFieldTwo.text != "" && self.textFieldThree.text != "" && self.textFieldFour.text != ""   {
...
}

and we can also do this with Guard

guard let text = self.myTextField.text  where !text.isEmpty else {
return
}

Using If-Let and checking the output in a single line

You can use where clause:

if let value = Double(textFieldText) where value > 0 {

Another option using nil coalescing operator:

if Double(textFieldText) ?? -Double.infinity > 0 {

Thanks to comments below which help me realize nil > 0 doesn't throw an error:

if Double(textFieldText) > 0 {

is by far the simplest option.

Is it possible to use the variable from an optional binding within the same conditional statement?

You can do it this way:

if let popupButton = result?.control as? NSPopUpButton, popupButton.numberOfItems <= 1 {
//blahblah
}


Related Topics



Leave a reply



Submit