Swift Identifiers

Swift Identifiers

Identifiers name variables, constants, types, functions, etc.


Rules for Identifiers

Identifiers must:

  • Start with a letter or underscore.
  • May include numbers after the first character.
  • Avoid keywords unless escaped with backticks.

They can contain Unicode and must not start with a number.

Example

let name = "Swift"
let π = 3.14
let _hidden = true


Escaping Keywords and Unicode

Escape keywords with backticks when needed, and remember identifiers can include Unicode characters.

Example

let switch = "ok"   // escape a keyword using backticks
let 🐶 = "dog"          // Unicode identifier
print(switch, 🐶)

Use backticks to escape reserved words in identifiers, and remember Swift identifiers can include Unicode characters.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *