Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift variable name with ` (backtick)

I was browsing Alamofire sources and found a variable name that is backtick escaped in this source file

open static let `default`: SessionManager = {     let configuration = URLSessionConfiguration.default     configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders      return SessionManager(configuration: configuration) }() 

However in places where variable is used there are no backticks. What's the purpose of backticks?

Removing the backticks results in the error:

Keyword 'default' cannot be used as an identifier here

like image 679
user3237732 Avatar asked Jan 06 '17 10:01

user3237732


1 Answers

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.

like image 79
Ortomala Lokni Avatar answered Oct 05 '22 22:10

Ortomala Lokni