Multiple conditions in guard statement in Swift

Swift

Swift Problem Overview


Is there a way to include multiple conditions in a guard statement of Swift?

For example, if I want to check two optional values are nil using a guard, how should I do it using single guard statement?

Swift Solutions


Solution 1 - Swift

Check this code

func demo(){

    var str = [String: String]()

    str["status"] = "blue"
    str["asd"] = nil

    guard let var2 = str["asd"], let var1 = str["status"]
    else
    {
        print("asdsfddffgdfgdfga")
        return
    }
    print("asdasdasd")
}

Guard will check one by one condition. If the first is true then it will check the next. Otherwise, it will execute the else part.

Solution 2 - Swift

To answer Prabhav's question, yes, you are correct, each condition in a guard statement must be true in order to proceed (i.e., not go into the else block). In this sense, it is indeed like separating conditions with AND logic.

You can implement OR logic, not by using commas, but by using a Boolean condition:

guard
    true || false    // this guard statement evaluates to true
else
    {
    print("no, not all values in the guard were true")
    return
    }

print("yes, all of the values in the guard were true")  // this is printed

or a combination of OR and AND logic, by using a combination of Boolean conditions and optional bindings:

let testString: String? = nil

guard
    true || false,
    let x = testString,    // this guard statement evaluates to false
    true
else
    {
    print("no, not all values in the guard were true")  // this is printed
    return
    }

print("yes, all of the values in the guard were true")

This summary from Apple, written about optional bindings in if statements is equally applicable to guard statements:

> You can include as many optional bindings and Boolean conditions in a > single if statement as you need to, separated by commas. If any of the > values in the optional bindings are nil or any Boolean condition > evaluates to false, the whole if statement’s condition is considered > to be false. The following if statements are equivalent:

if let firstNumber = Int("4"), let secondNumber = Int("42"),
    firstNumber < secondNumber && secondNumber < 100
    {
    print("\(firstNumber) < \(secondNumber) < 100")
    } // Prints "4 < 42 < 100"

if let firstNumber = Int("4")
    {
    if let secondNumber = Int("42")
        {
        if firstNumber < secondNumber && secondNumber < 100
            {
            print("\(firstNumber) < \(secondNumber) < 100")
            }
        }
    } // Prints "4 < 42 < 100"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKishorethejuView Question on Stackoverflow
Solution 1 - SwiftRutvik KanbargiView Answer on Stackoverflow
Solution 2 - SwiftGene LoparcoView Answer on Stackoverflow