我在浏览 Alamofire 源代码时发现在 这个源文件
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?
删除反引号导致错误:
关键字default"不能在这里用作标识符
Keyword 'default' cannot be used as an identifier here
推荐答案根据 Swift 文档:
要使用保留字作为标识符,请在其前后加上反引号 (`).例如,class 不是有效的标识符,但 `class` 是有效的.反引号不被视为标识符的一部分;`x` 和 x 意义相同.
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.
在您的示例中,default 是 Swift 保留关键字,这就是需要反引号的原因.
In your example, default is a Swift reserved keyword, that's why backticks are needed.
更多推荐
带有 `(反引号)的 Swift 变量名
发布评论