Chapter 13: Swift Identifiers

1. What is an identifier in Swift?

An identifier is simply the name you give to:

  • variables (let / var)
  • constants
  • functions
  • types (structs, classes, enums, protocols…)
  • parameters
  • properties
  • labels
  • etc.

Basically — almost everything you name yourself in code is an identifier.

Examples of identifiers:

Swift

2. Basic rules — what characters are allowed?

Swift has quite flexible but clear rules for identifiers.

Allowed characters:

  1. Letters (Unicode letters — very broad!)
    • English: a–z, A–Z
    • Accented letters: é, ñ, ç, ü, ā, ś, etc.
    • Many other scripts: Devanagari, Bengali, Tamil, Arabic, Chinese, Japanese, Korean, etc.
  2. Digits0–9 → but cannot be the first character
  3. Underscore _
  4. Emoji (yes — really!)
  5. Combining characters (diacritics, etc.)

Not allowed:

  • Spaces
  • Most symbols: @, #, $, %, &, *, etc.
  • Hyphen – (very common mistake coming from CSS/HTML)
  • Starting with a digit

3. Real examples — valid and invalid identifiers

Swift

4. Keywords — names you cannot use

Swift has reserved keywords that you cannot use as identifiers (unless you escape them with backticks — more on that later).

Common keywords you’ll bump into:

Swift

5. Using backticks to escape keywords (rare but useful)

You can use a keyword as an identifier by wrapping it in backticks

Swift

Realistic use cases (not very common but they exist):

  • Interfacing with C/Objective-C APIs where a keyword is used
  • Writing code generators
  • Porting code from other languages
  • Teaching/learning (to show escaping)

Most Swift developers try to avoid this — it’s better to rename the variable.

6. Naming conventions — what real Swift developers actually do

Kind of identifier Convention (2025–2026 style) Examples
Variables & constants camelCase userName, currentTemperature, maxRetries
Types (struct, class, enum, protocol) UpperCamelCase UserProfile, NetworkManager, HTTPStatus
Functions & methods camelCase fetchUserData, calculateTotalPrice
Constants (especially global/static) sometimes UPPER_CASE API_KEY, MAX_UPLOAD_SIZE
Private properties Often camelCase + private private var internalCache
Parameters camelCase userID, newPassword
Associated values in enums camelCase case success(message: String)

7. Very common beginner mistakes (and better alternatives)

Mistake Bad example Better / correct style Why better?
Using hyphens max-size maxSize Hyphens not allowed
Starting with number 2ndPlayer secondPlayer or player2 Rule violation
Using keyword without backticks let class = "Premium" let userClass = "Premium" or let class` = … Avoids confusion & backticks
Very long unreadable names thisIsTheCurrentUserSelectedProfilePictureUrl profilePictureURL Readability & maintainability
Inconsistent casing User_name, userName, username Pick one style (usually camelCase) Team consistency & clarity
Overly short / cryptic names x, tmp, a1 index, temporaryValue, userAge Future you will thank you

8. Practical examples — clean, real-world naming

Swift

9. Quick reference – allowed vs not allowed

Type of character First character? Later characters? Example
English letter Yes Yes userName
Digit No Yes player2
Underscore _ Yes Yes _cache, max_count
Emoji Yes Yes 😺Counter, 🚀Launch
Accented letter Yes Yes caféPrice, straßeLength
Non-Latin script Yes Yes नाम, 価格
Hyphen – No No ❌ max-size
Space No No
Most symbols (@#$%) No No

Would you like to go deeper into any of these next?

  • Naming conventions in more detail (SwiftUI vs backend vs UIKit)
  • When and why to use backticks
  • How to name variables vs functions vs types
  • Common anti-patterns in naming
  • Or move to another topic (variables, constants, printing, optionals…)

Just tell me what feels most useful right now — we’ll keep the same detailed, teacher-like style 😊

You may also like...

Leave a Reply

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