Brief Introduction About Swift
Swift is a language developed by Apple which is made using modern approach, include safety features and software design patterns. As the name suggests swift is fast, safe and easy to use. It is basically made a replacement for c-based family(C, C++ and Objective-C). It can be used to make apps and can be also used for cloud services and it is among the fastest growing languages.
Guard Statements in Swift
A guard statement is used to transfer program control out of a scope if one or more conditions aren’t met. The value of any condition in a guard statement must be of type Bool or a type bridged to Bool. They are generally used when we want to escape out of code if statement didn't match the condition and do not go further.
Syntax For Guard Statements
guard <#condition#> else {
<#statements#>
}
How to Use Guard and Their Control Flow in Code
//Guards in Swift
func isValidPassword(password : String) -> Bool {
if password.count > 6 {
return true
}
else {
return false
}
}
func passwordTester(passwordToBeChecked : String) {
guard isValidPassword(password: passwordToBeChecked) else {
print("Password is smaller than 6 characters") //This will execute
return
}
print("Password is Valid")
}
passwordTester(passwordToBeChecked: "Xcode")
Now, let's pass password greater than 6 characters
//Guards in Swift
func isValidPassword(password : String) -> Bool {
if password.count > 6 {
return true
}
else {
return false
}
}
func passwordTester(passwordToBeChecked : String) {
guard isValidPassword(password: passwordToBeChecked) else {
print("Password is smaller than 6 characters")
return
}
print("Password is Valid") // This will be executed
}
passwordTester(passwordToBeChecked: "Xcode12")
Why To Use Guards
I know that we can use if-else statements to carry out this task but let's first see some code then we will judge both approaches
//Guards in Swift
func isValidPassword(password : String) -> Bool {
if password.count > 6 {
return true
}
else {
return false
}
}
func passwordTester(passwordToBeChecked : String) {
guard isValidPassword(password: passwordToBeChecked) else {
print("Password is smaller than 6 characters")
return
}
print("Password is Valid")
// if isValidPassword(password: passwordToBeChecked) {
// print("Is Vaid")
// }else {
// print("is not valid")
// }
print("This statement is executed")
}
passwordTester(passwordToBeChecked: "Xcode")
//Output : Password is smaller than 6 characters
Here in above example as password is less than 6 characters so it will exit beforehand without executing any other statement but password have been greater than 6 characters then
//Guards in Swift
func isValidPassword(password : String) -> Bool {
if password.count > 6 {
return true
}
else {
return false
}
}
func passwordTester(passwordToBeChecked : String) {
guard isValidPassword(password: passwordToBeChecked) else {
print("Password is smaller than 6 characters")
return
}
print("Password is Valid")
// if isValidPassword(password: passwordToBeChecked) {
// print("Is Vaid")
// }else {
// print("is not valid")
// }
print("This statement is executed")
}
passwordTester(passwordToBeChecked: "Xcode12")
//Output: Password is Valid
//This statement is executed
Only when guard got true it executed below statements. Now let's see in case of if-else
//Guards in Swift
func isValidPassword(password : String) -> Bool {
if password.count > 6 {
return true
}
else {
return false
}
}
func passwordTester(passwordToBeChecked : String) {
// guard isValidPassword(password: passwordToBeChecked) else {
// print("Password is smaller than 6 characters")
// return
// }
// print("Password is Valid")
if isValidPassword(password: passwordToBeChecked) {
print("Password Is Valid")
}else {
print("Password is less than 6 characters")
}
print("This statement is executed")
}
passwordTester(passwordToBeChecked: "Xcode12")
//Output: Password Is Valid
//This statement is executed
In above case if executed and now let's see for else
//Guards in Swift
func isValidPassword(password : String) -> Bool {
if password.count > 6 {
return true
}
else {
return false
}
}
func passwordTester(passwordToBeChecked : String) {
// guard isValidPassword(password: passwordToBeChecked) else {
// print("Password is smaller than 6 characters")
// return
// }
// print("Password is Valid")
if isValidPassword(password: passwordToBeChecked) {
print("Password Is Valid")
}else {
print("Password is less than 6 characters")
}
print("This statement is executed")
}
passwordTester(passwordToBeChecked: "Xcode")
//Output:Password is less than 6 characters
//This statement is executed
Here in this case the below statement print("This statement is executed")
is always executing so here we can see that guard is first checking the condition and exiting us out of function without proceeding any further but in if else we are executing all statements.
We can use any approach it is something recommended by Apple to use in case you want early exit from function.
So, this is it from my side on guards. If you guys have any suggestions please comment below.
Top comments (0)