Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for nil with guard instead of if?

Tags:

Is there a guard equivalent of checking if a variable is nil? If so how would I translate a statement like this to use guard instead?

if post["preview"]! != nil {     //do stuff } else {     //handle case where the variable is nil } 
like image 494
yesthisisjoe Avatar asked Mar 31 '16 18:03

yesthisisjoe


People also ask

When to use if let and guard?

in other words, "guard let" is used when the code is 99% sure of not using the else conditional; in the other hand, "if let" when the code is 50 - 50(example) to use else condition. The variable bound by if let is only visible inside if let scope. The variable bound by guard let is visible afterwards.

What is the difference between if let and guard let in Swift?

In if let , the defined let variables are available within the scope of that if condition but not in else condition or even below that. In guard let , the defined let variables are not available in the else condition but after that, it's available throughout till the function ends or anything.

Why we use guard let in Swift?

In Swift, we use the guard statement to transfer program control out of scope when certain conditions are not met. The guard statement is similar to the if statement with one major difference. The if statement runs when a certain condition is met. However, the guard statement runs when a certain condition is not met.

How does guard let work?

Swift gives us an alternative to if let called guard let , which also unwraps optionals if they contain a value, but works slightly differently: guard let is designed to exit the current function, loop, or condition if the check fails, so any values you unwrap using it will stay around after the check.


2 Answers

Like some people already answered, you can use let

guard let preview = post["preview"] else { /* Handle nil case */ return } 

If you are not using the variable, you can use an underscore to not declare the variable and avoid the warning.

guard let _ = post["preview"] else { /* Handle nil case */ return } 

You can also do a regular boolean check instead of using let

guard post["preview"] != nil else { /* Handle nil case */ return } 

A more general case for a boolean check on a guard

guard conditionYouExpectToBeTrue else { /* Handle nil case */ return } 

If you want to be able to modify the variable, you can use var instead of let

guard var preview = post["preview"] else { /* Handle nil case */ return } 

Swift 3.0

You can combine var/let with a boolean check by using commas between the statements.

guard let preview = post["preview"], preview != "No Preview" else { /* Handle nil case */ return } 

Swift 2.x

You can combine var/let with the boolean check by using where where

guard let preview = post["preview"] where preview != "No Preview" else { /* Handle nil case */ return } 
like image 138
EmilioPelaez Avatar answered Oct 19 '22 09:10

EmilioPelaez


You can use guard to grab the value from the post dictionary:

guard let value = post["preview"] else {     return  // or break, or throw, or fatalError, etc. } // continue using `value` 
like image 44
jtbandes Avatar answered Oct 19 '22 08:10

jtbandes