The guard statement

In Swift, and most modern languages, our conditional statements tend to focus on testing if a condition is true. As an example, the following code tests to see whether the variable x is greater than 10 and; if so, we will perform some function. If the condition is false, we handle the following error condition:

var x = 9  
if x > 10 { 
  // Functional code here 
} else { 
  // Do error condition 
} 

This type of code embeds our functional code within our checks and tucks the error conditions away at the end of our functions, but what if that is not what we really want? Sometimes, it might be nice to take care of our error conditions at the beginning of the function. In our simple example, we could easily check if x is less than or equal to 10 and; if so, it will perform the error condition. Not all conditional statements are that easy to rewrite, especially items such as optional binding.

In Swift, we have the guard statement. This statement focuses on performing a function if a condition is false; this allows us to trap errors and perform the error conditions early in our functions. We could rewrite our previous example using the guard statement like this:

var x = 9 
guard x > 10 else { 
  // Do error condition  
  return 
} 
//Functional code here 

In this new example, we check to see whether the variable x is greater than 10, and if not, we perform the error condition. If the variable is greater than 10, the code continues. You will notice that we have a return statement embedded within the guard condition. The code within the guard statement must contain a transfer of control statement; this is what prevents the rest of the code from executing. If we forget the transfer of control statement, Swift will show a compile time error.

Let's look at some more examples of the guard statement. The following example shows how we would use the guard statement to verify that an optional contains a valid value:

func guardFunction(str: String?) {  
  guard let goodStr = str else { 
    print("Input was nil")  
    return 
  } 
  print("Input was (goodStr)") 
} 

In this example, we create a function named guardFunction()that accepts an optional that contains a string or nil value. We then use the guard statement with optional binding to verify that the string optional is not nil. If it does contain nil, then the code within the guard statement is executed and the return statement is used to exit the function. The really nice thing about using the guard statement with optional binding is that the new variable is in the scope of the rest of the function, rather than just within the scope of the optional binding statement.

A conditional statement checks the condition once and; if the condition is met, it executes the block of code. What if we wanted to continuously execute the block of code until a condition is met? For this, we would use one of the looping statements in Swift.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset