Lesson Notes and Walkthrough
A variable is a named storage location for data. In plain language, it is like a labeled box: you put a value in, and later you can read it or replace it with a new value.
Why Variables Matter
- They let your app remember information such as a username, score, or message.
- They make code easier to read because names describe what data means.
- They allow values to change as users interact with your app.
Basic Swift Syntax
var variableName = value Example: var score = 0
Here, var means the value can change, score is the name, and 0 is the starting value.
Step-by-Step Walkthrough
- Create a variable and give it an initial value.
- Update the variable when something changes.
- Print the value to verify your logic.
var points = 10
points = 15
print(points) Output: 15
Naming Tips for Beginners
- Use clear names:
userNameis better thanx. - Use camelCase in Swift:
totalPrice,isLoggedIn. - Name by meaning, not type. Prefer
cartCountoverint1.
Common Mistakes to Avoid
- Forgetting to use
varwhen you plan to change the value later. - Using vague names that make code hard to understand.
- Changing too many variables in one place without checking output.
Quick check: if the value may change, start with var. If it should never change, use let (covered in Part 5).