Infinite Possibilities Infinite Possibilities - 1 year ago 111
Swift Question

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 {

Before this, I was using
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?

Answer Source

Swift Programming Language

Presents a note that say 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.


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download