Guard
Guard
Introduction
Hello,
In this lesson, we will learn about the guard, an expression we will use frequently.
The use of guard statement is similar to if. A guard statement is used to transfer program control out of a scope if one or more conditions aren’t met.
It helps to get rid of if statement in some situations
As an extra with respect to if, the guard statement always has its else statement, and this else statement works if the condition is met.
cont.
Unlike if, it creates constants that can be used outside the block.
It is especially used to unwrap optional ones.
In the example below, the person's dictionaries have been unwrapped and assigned to the constant name.
The name constant is used outside the block to which it belongs.