Chapter 12: Swift Multiple Variables
1. The different ways to declare multiple variables
Swift gives you several clean and expressive ways to declare more than one variable at once.
Style 1 – Separate lines (most common & most readable)
|
0 1 2 3 4 5 6 7 8 9 10 |
let firstName = "Aarav" let lastName = "Sharma" let age = 19 let city = "Hyderabad" let isStudent = true |
Why this is usually the best choice:
- Very easy to read
- Easy to change one value without touching others
- Easy to add comments
- Easy to see types (if you add them)
- Git diffs are clean when you change only one variable
Style 2 – Multiple declarations on one line with commas
|
0 1 2 3 4 5 6 7 |
let width = 320, height = 568, scale = 2.0 var score = 0, lives = 3, coins = 150 |
Important rules:
- All variables on the same line must have the same type (unless you write types explicitly)
- You can mix let and var — but it’s unusual and often confusing
|
0 1 2 3 4 5 6 7 |
// This is allowed but not recommended (looks strange) let name = "Priya", var count = 0, let max = 100 |
→ Recommendation: avoid mixing let and var on one line
Style 3 – Multiple variables with explicit types
|
0 1 2 3 4 5 6 7 8 |
let width: CGFloat = 375, height: CGFloat = 812 var red: UInt8 = 255, green: UInt8 = 128, blue: UInt8 = 0 let minValue: Int = 0, maxValue: Int = 100, step: Int = 5 |
This is common when:
- You are working with very specific types (colors, coordinates, fixed-width integers…)
- You want to make the types very clear at a glance
Style 4 – Multiple variables with the same value
|
0 1 2 3 4 5 6 7 8 9 |
let defaultPadding: CGFloat = 16 let smallPadding = defaultPadding let mediumPadding = defaultPadding let largePadding = defaultPadding * 2 |
Shorter (and very common) way:
|
0 1 2 3 4 5 6 7 8 |
let defaultPadding: CGFloat = 16 let smallPadding, mediumPadding, largePadding: CGFloat (smallPadding, mediumPadding, largePadding) = (defaultPadding, defaultPadding, defaultPadding * 2) |
But most people just write:
|
0 1 2 3 4 5 6 7 8 |
let smallPadding = 8 let mediumPadding = 16 let largePadding = 24 |
→ Clear is usually better than clever
2. Real-world patterns – how multiple variables are actually used
Pattern A – Related constants (very common)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Design constants let cornerRadius: CGFloat = 12 let shadowRadius: CGFloat = 4 let shadowOpacity: Float = 0.15 let animationDuration = 0.3 // API limits let maxUsernameLength = 30 let maxBioLength = 160 let maxPostsPerPage = 20 |
Pattern B – Game / score related
|
0 1 2 3 4 5 6 7 8 9 10 |
var currentScore = 0 var highScore = 0 var livesRemaining = 3 var coinsCollected = 0 var level = 1 |
Pattern C – Color components (very common in UI code)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
let red = 0.92 let green = 0.34 let blue = 0.12 let alpha = 1.0 // or more clearly: let backgroundRed: CGFloat = 0.98 let backgroundGreen: CGFloat = 0.96 let backgroundBlue: CGFloat = 0.94 |
Pattern D – Position / layout values
|
0 1 2 3 4 5 6 7 8 9 10 11 |
let screenWidth = UIScreen.main.bounds.width let screenHeight = UIScreen.main.bounds.height let safeAreaTop: CGFloat = 44 // iPhone with notch let safeAreaBottom: CGFloat = 34 let navBarHeight: CGFloat = 44 |
3. Very common beginner mistakes (and better alternatives)
| Bad / confusing style | Why it’s problematic | Better & clearer way |
|---|---|---|
| var a=0,b=0,c=0 | Hard to read, no spaces, looks messy | var a = 0, b = 0, c = 0 or separate lines |
| let x=10,y=20,z=”hello” | Mixing types on one line without type annotation | Separate lines or add types explicitly |
| let name=”Rahul”, age=25, city=”Delhi” | Looks like one long variable name | Add spaces: let name = “Rahul”, age = 25 |
| var x,y,z = 0 | Only z becomes 0 — x and y are uninitialized | var x = 0, y = 0, z = 0 |
| Declaring 10 variables in one line | Very hard to read & maintain | Group related variables, use separate lines |
4. Good habits for declaring multiple variables
- Group related variables together
- Align them visually when it helps readability
- Use consistent spacing
- Prefer separate lines for more than 3–4 variables
- Add comments when there are many constants
Example of clean, real code style:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// MARK: - Layout Constants let standardMargin: CGFloat = 16 let smallMargin: CGFloat = 8 let largeMargin: CGFloat = 24 let buttonHeight: CGFloat = 48 let cornerRadius: CGFloat = 12 let iconSize: CGFloat = 24 // MARK: - Animation let shortAnimationDuration = 0.25 let mediumAnimationDuration = 0.4 |
5. Mini practice – improve these declarations
Try to rewrite these in a cleaner, more Swift-idiomatic way:
|
0 1 2 3 4 5 6 7 8 |
var name="Sneha",age=22,city="Bengaluru",isActive=true let max=100,min=0,step=5 var r=255,g=128,b=0 |
Possible better versions:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
let name = "Sneha" let age = 22 let city = "Bengaluru" let isActive = true let maxValue = 100 let minValue = 0 let stepSize = 5 var red = 255 var green = 128 var blue = 0 |
Would you like to go deeper into one of these next?
- How to print multiple variables nicely
- Multiple variables in loops / for-in
- Destructuring (tuple unpacking)
- Multiple variables in function parameters
- When to group constants into a struct / enum
Just tell me what feels most useful right now — we’ll continue in the same detailed, teacher-like style 😊
