Swift Variable Name With ' (Backtick)

Swift variable name with ` (backtick)

According to the Swift documentation :

To use a reserved word as an identifier, put a backtick (`)before and after it. For example, class is not a valid identifier, but `class` is valid. The backticks are not considered part of the identifier; `x` and x have the same meaning.

In your example, default is a Swift reserved keyword, that's why backticks are needed.

What does the `` mean in swift and why is it useful?

The backticks allow you to use restricted keywords in places where they otherwise couldn't be used, such as variable names.

default is a restricted keyword in Swift, but by putting backticks around it, it becomes a valid variable name.

Without the backticks, your code would result in the error

Keyword 'default' cannot be used as an identifier here

What is `self` in swift?

self is a keyword and normally you cannot use keywords and reserved words in places outside their context. However, that sometimes creates problems. For that reason there is a special syntax to make the keyword to be a normal identifier, e.g.:

enum MyEnum: String {
case `default`
}

(also see Swift variable name with ` (backtick))

Historically self was not allowed as as a constant name inside guard-let-else and therefore backticks were commonly (ab)used.
They are no longer needed since Swift 4.2.

The code will still work correctly since you can wrap any identifier in backticks and it will just be a normal identifier.

Practical application of backticks in Swift

The most important usage is the interaction with other languages that have different keywords.

From Swift you can call C and Obj-C functions.

Now, consider for example that you need to call a C function called guard. However, that's a keyword in Swift, therefore you have to tell the compiler that you don't want to use it as a keyword but as an identifier, e.g.:

`guard`()

There are multiple keywords in Swift that are widely used as method/function names, e.g. get and set. For many contexts Swift is able to figure out the difference but not always.

How to use Objective-C classes with names which are keywords in Swift

You have to use backticks:

fetchCondition.sqlite.`where` = @"name = 'sample1' and price > 50";

To use a reserved word as an identifier, put a backtick (`) before and after it. For example, class is not a valid identifier, but `class` is valid. The backticks are not considered part of the identifier; `x` and x have the same meaning.

The Swift Programming Language – Lexical Structure

Use reserved keyword a enum case

From the Swift Language Guide (Naming Constants & Variables section)

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

enum MyEnum {
case `Self`
case AnotherCase
}

and use it with or without backticks

let x: MyEnum = .Self
let y = MyEnum.`Self`

Swift Codable with reserved word

I've had similar problems with 'return'. You can get around with CodingKeys.

public struct PhoneNumber: Codable {
enum CodingKeys: String, CodingKey {
case phoneNumber
case extensionString = "extension"
case isPrimary
case usageType
}

var phoneNumber: String
var extensionString: String
var isPrimiry: Bool
var usageType: Int
}

As you cant call a property 'extension' you name it something similar but use the CodingKeys to tell you object what the key in the JSON is.

IBOutlet variable name surrounded by ( ` )?

Because continue is a Swift reserved word. You can't use a reserved word as a variable name without surrounding it with backticks.

`self` in Swift closure

self is a reserved word in Swift. Since you're creating a new local var called self you need to mark it with back-ticks, as explained in the link from rmaddy.

Note that the usual convention for mapping weak self to a strong var is to use the name strongSelf:

() -> Void = { [weak self] in
guard let strongSelf = self else {
//your code to call self.callMethod2() can't succeed inside the guard (since in that case weak self is nil)
//self.callMethod2()
return //You must have a statement like return, break, fatalEror, or continue that breaks the flow of control if the guard statement fails
}
strongSelf.callMethod3()
}


Related Topics



Leave a reply



Submit