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.

Before we deep dive into guard let us understand what are Optionals,

As per Apple’s Documentation: Optionals are a type that represents either a wrapped value or nil, the absence of a value.

Optional in Swift means the possibility of absence of a value for variable or constant.
In general a variable/constant is not allowed to have nil value, unless it is explicitly mentioned that the variable can go nil. For example

An optional value either contains some value or contains nil to indicate that a value is missing. Write a question mark (?) after the type of a value to mark the value as optional.

let a : Int = nil

will not compile.

How to declare optionals

Declaring optionals is very simple. You just need to write the type with a trailing question mark (?)

var a : Int?

Ways to Unwrap Optionals in Swift

Now since we’ve seen optionals, let us see ways to unwrap it.

Force Unwrapping:

This is the simplest way to unwrap an optional. You need to just put exclamation mark (!) after the optional value and you get the unwrapped value.

var a : Int? = 10
print(a!) // 10

Safely Unwrapping by Checking for nil

var a :Int?
var b = 0// 
safety check
if a !=nil {
    b = a!
}

if let binding

if let variable1 = optional1 {
      //use variable1 here
} else {
     // optional1 was holding nil thats why else block is executing
}

The third way is using guard which we will be seeing in this post.

For more in depth you can check out https://developer.apple.com/documentation/swift/optional but as of now having above understanding is sufficient.

Now Since we got some understanding on Optionals let us see Guard in Swift.

guard statement is used to transfer program control out of a scope if one or more conditions aren’t met.

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.



Categories: iOS, Swift

Tags: , , ,

3 replies

Trackbacks

  1. Custom Types As Raw Value for Enum in Swift – Swift With Sadiq
  2. Implicitly Unwrapped Optionals in Swift – Swift With Sadiq
  3. Ways to Unwrap Optionals in Swift – Swift With Sadiq

Leave a Reply

Please log in using one of these methods to post your comment:

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

%d bloggers like this: