"Testing1"
Swift Constants
What Are Constants?
Constants are like permanent labels.Once you lock a value into a constant, it stays frozen for the rest of your code — no edits allowed.They're perfect when you know the value should stay the same.
Declaring a Constant
Use the keyword let to define a constant.
let pi = 3.14 Let language = "Swift"
- pi and language will always have the same values.
- Trying to change them later will cause an error.
Why Use Constants?
- Safeguard your values – ensures they stay untouched throughout execution.
- Makes your code easier to understand
- Improves performance (Swift can optimize fixed values)
Type Inference
Swift guesses the type from the assigned value:
let maxUsers = 100 // Int let siteName = "Apple" // String Let isLoggedIn = false // Bool
But you can be specific if needed:
let rating: Double = 4.9 Let isVerified: Bool = true
Constant vs Variable
| Feature | let (Constant) | var (Variable) |
|---|---|---|
| Can change? | No | Yes |
| Safe to reuse? | Always | Be careful |
| Performance | Optimized | Flexible |
Real-Life Analogy
Think of a constant as your birth year — it never changes.
let birthYear = 1995
Prefer Learning by Watching?
Watch these YouTube tutorials to understand GIT Tutorial visually:
What You'll Learn:
- 📌 Swift Basics - Variables and Constants
- 📌 How to use Variables and Constants | Swift Basics #3