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)

Swift

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

Swift

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
Swift

Recommendation: avoid mixing let and var on one line

Style 3 – Multiple variables with explicit types

Swift

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

Swift

Shorter (and very common) way:

Swift

But most people just write:

Swift

→ Clear is usually better than clever

2. Real-world patterns – how multiple variables are actually used

Pattern A – Related constants (very common)

Swift

Pattern B – Game / score related

Swift

Pattern C – Color components (very common in UI code)

Swift

Pattern D – Position / layout values

Swift

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:

Swift

5. Mini practice – improve these declarations

Try to rewrite these in a cleaner, more Swift-idiomatic way:

Swift

Possible better versions:

Swift

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 😊

You may also like...

Leave a Reply

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