Guard in Swift

guard statement is used to transfer program control out of a scope if one or more conditions aren’t met also called as early exit.  i.e. Either you meet the condition asked, or get out of the scope.

guard let is also an alternative to if let, which also unwraps optionals. guard let will unwrap an optional for you and if it finds nil inside, it expects you to exit the function, loop, or condition you used it in.

The else clause of a guard statement is required, and must either call a function with the Never return type or transfer program control outside the guard statement’s enclosing scope using one of the return, break, continue or throw statement.

Syntax:  
guard condition else { 
statements 
}

Here the condition which you are evaluating is boolean which will return true or false, if it returns false the statements inside the else is executed, if it returns true the statements inside the if blocked are skipped.

So, now we’ve got the basics let’s dig into the real time examples.

Example 1:

func validate() {         
    guard 3>2 else {             
    print ("False")             
    return         
    }         
    print ("True") //True     
} 
validate()

In the above example we see that 3 is greater than 2 and the statement inside the guard else clause are skipped and True is printed.

Example 2:

func validate() {         
    guard 1>2 else {             
    print ("False")            //False 
    return         
    }         
    print ("True")      
} 
validate()

In the above example we see that 1 is smaller than 2 and the statement inside the guard else clause are executed and False is printed followed by return.

Example 3: gaurd let, unwrapping optionals through guard let

func getName(args myName: String?) {
     guard let name = myName, !name.isEmpty else {
     print ("Condition is false")          // Condition is false            return         
     }         
     print("Condition is met\(name)")     
} 
getName(args: "")
 

In the above example we are using guard let to unwrap the optionals. In the function getName we’ve defined a variable of type string myName which is optional. We then use guard let to check whether the variable myName is nil or not, if not assign to name and check again, name is not empty. If both the conditions qualified i.e. true the else block will be skipped and print “Conditions is met with name”.

Basically we are checking two things here separated by comma, first unwrapping and optional and checking whether that satisfies condition or not.

Here we are passing nothing to the function i.e. empty string and hence Condition is false is print.

func getName(args myName: String?) {
     guard let name = myName, !name.isEmpty else {
     print ("Condition is false")          
     return         
     }        
     print("Condition is met \(name)") // Condition is met Aditya    } getName(args: "Aditya")

Here we are passing “Aditya” to the function and you can see the output is printed “Condition is met Aditya”.

Now the most important part

If let vs guard let? Which is better ?

With the help of both guard and if let we can unwrap the variable, however if we want to use the variable out side the scope the gaurd let serves the purpose but if let cannot. It means in both the cases we create one variable the scope of variable in guard let is outside but not for if let. Guard let help in early exit of the function and does not traverse the remaining code.

I hope this was pretty easy? huh! I’ve seen newbie programmer struggling with this a lot, probably not if you’ve gone through it.

Do let me know in comments if you have any queries or doubts.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s