skip to Main Content

I found this code in one of the old projects:

guard let `self` = self else {
     return .empty()
}

static let `default`: LayoutParameters = { ..some code.. }

I assume “ was used in older versions of the language. But I would like to know why it is used/was used. Also, I would like to know if there are any problems if do not change the code and "leave it as is" in the latest versions of Swift and Xcode. Does it work correctly? Should I replace this with

guard let self = self else ......

2

Answers


  1. The Xcode IDE suggestion you using to help to use the same default key in Foundation SDK.

    Example: default is a constant name in Foundation, if you want using default to create new variable name is default you need add .

    But you using SwiftLint with default rules, Using a default contants name is a code smell.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search