Swift Identifiers

🆔 Swift Identifiers (Names in Swift)
Simply put 👉 Identifiers are names given to program elements.
What Are Identifiers Used For?
Identifiers name:
Variables
Constants
Functions
Classes / Structs / Enums
Protocols
Properties
Example:
Here, age, pi, and calculateSum are identifiers.
Rules for Swift Identifiers (Very Important ⚠️)
✅ Allowed
Can start with:
A letter (
a–z,A–Z)An underscore (
_)
Can contain:
Letters
Numbers
Underscore
_
Unicode characters are allowed (but not recommended)
❌ Not Allowed
Cannot start with a number
Cannot contain spaces
Cannot use Swift keywords directly
Swift Is Case-Sensitive
✔ age and Age are different identifiers
Using Keywords as Identifiers (Backticks Trick 🔥)
It allows keywords as identifiers using backticks.
✔ Works, but not recommended in real projects
Identifier Naming Conventions (Best Practices ✅)
Camel Case (Recommended)
Pascal Case (Types Only)
Avoid This ❌
Valid vs Invalid Identifiers
| Identifier | Valid? |
|---|---|
totalMarks | ✅ |
_tempValue | ✅ |
user123 | ✅ |
123user | ❌ |
user-name | ❌ |
func | ❌ |
Identifiers in Different Contexts
Variable
Constant
Function
Class
🧠 Summary
Identifiers are names of program elements
Swift identifiers are case-sensitive
Must follow strict naming rules
Use camelCase for variables & functions
Avoid keywords and confusing names
