Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift guard with self

I am doing the weak strong dance in swift this way:

dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), { [weak self] in
    guard let `self` = self else {
        return
    }
    self.doSomething(1)
})

Before this, I was using strongSelf instead of `self`. On a website I've seen that I can use this character ` .

But what does this character do in Swift? Without this I cannot assign to self. Why does this work? Is it a good practice to use it?

like image 405
Infinite Possibilities Avatar asked Sep 07 '16 05:09

Infinite Possibilities


People also ask

What is swift guard and how to use it?

What is Swift Guard? A guard statement is as simple as using an if..else statement and has its own benefits and uses. Swift guard is defined as a statement that is used to transfer program control out of a scope if one or more conditions aren’t met.

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

The main difference between guard and if statements in swift are: The if statement is used to run code when a condition is met. The guard statement is used to run code when a condition is not met. The main use case difference between the guard statement and a regular if statement is in the code readability.

What is the guard condition in Swift optionals?

That is, the guard condition is true only if both conditions are true. While working with Swift Optionals, the guard statement is used as the guard-let statement. For example, In the above example, we have created an optional variable named age. Here, we are using the guard-let statement to check whether age contains a value or not.

What are the multiple use cases for the Swift guard keyword?

The multiple use cases for the Swift guard keyword can be grouped into three main categories: Exit Early. Like the Guard Let Optional example, a guard statement can be used to exit early if an expected variable is an optional.


2 Answers

A bit of an update (I will not refer here when to use it but rather how).

From Swift 4.2 the use should be like:

guard let self = self else { return }

Using ` is basically based on the compiler bug thus not advised.

For more information there is no better source than this. Explaining all reasoning behind and other considerations.

In short the above is more consistent with other cases seen in the code like:

if let myVariable = myVariable

So does not create confusion/discrepancies.

like image 171
Julian Avatar answered Oct 07 '22 00:10

Julian


Swift Programming Language

Presents a note that says the following:

If you need to give a constant or variable the same name as a reserved Swift keyword, surround the keyword with backticks (`) when using it as a name. However, avoid using keywords as names unless you have absolutely no choice.

EDIT:

The way I do this is using any other name for example strongSelf like you previously did. Because in the end, both `self` and strongSelf will be some variable to act upon. So I suggest if we can use some other variable name that is fine.

like image 31
Vivek Molkar Avatar answered Oct 07 '22 01:10

Vivek Molkar