Assigning a new value
A variable declared with var can be given a new value at any time, using the assignment operator =. The new value replaces the old one:
var myVar = 5
myVar = 10
print(myVar)
10
The first line declares myVar and initialises it with 5. The second line has no var, so it declares nothing. It stores 10 in the variable that already exists. From then on, every use of myVar sees 10.
Copying one variable into another
The value on the right of = can come from another variable:
var myVar = 5
var myNum = 10
myVar = myNum
myNum = 20
print(myVar, myNum)
10 20
The assignment copies the current value of myNum, which is 10. Afterwards the two variables are independent: setting myNum to 20 leaves myVar at 10.
The new value can also be computed from the old one. The right side is evaluated first, then stored:
var age = 24
age = age + 10
age += 1
print(age)
35
Reassignment order: swapping two values
Because an assignment overwrites the old value, order matters. Swapping two variables needs a third place to hold one value while the other is copied:
var a = 1
var b = 2
let temp = a
a = b
b = temp
print(a, b)
2 1
Without temp, the line a = b would destroy the original a, and both variables would end up as 2. Swift also has a shortcut, the tuple assignment (a, b) = (b, a).
The exercise, step by step
The starter code prints a short profile, then reaches a marked area where your code goes, then prints it again ten years later:
var role = "Junior Developer"
var age = 24
var language = "Java"
The second set of print lines must show a person who is ten years older, is now a senior developer, and still codes in Java.
- Reassign
roleto the string"Senior Developer". There is novarin front, because the variable already exists. - Reassign
age. The age is 24 and ten years pass, so the new value is34. - Do nothing to
language. He never changed it, so the old value is already right.
Only the marked area changes, and the print calls stay as they are. The second group of prints reads the variables after your edits.
The solution in Swift
var role = "Junior Developer"
var age = 24
var language = "Java"
// Today
print("His age is \(age)")
print("He is a \(role)")
print("He codes in \(language)")
// ---------------------------------------
// Write your code here:
role = "Senior Developer"
age = 34
// ---------------------------------------
// Ten years later:
print("Ten years later, his age is \(age)")
print("He is a \(role)")
print("He still codes in \(language)")
The first three lines of output still show 24, Junior Developer and Java, because they ran before the reassignment. The last three show 34, Senior Developer and Java.
Swift details
- Do not repeat
var. Writingvar role = "Senior Developer"a second time in the same scope is an error:invalid redeclaration of 'role'. - The type stays the same.
agewas inferred as anInt, soage = "old"does not compile:cannot assign value of type 'String' to type 'Int'. - Only
varcan be reassigned. A name declared withletis a constant, and assigning to it is a compile error. - Order matters. Code runs from top to bottom, so a line that reads a variable sees whatever value the most recent assignment left there.
Common mistakes
- Declaring again instead of assigning, with
varin front of the new value. - Forgetting to update a related value. Changing
rolebut notageleaves the story half updated. - Changing
languageas well. The task says it stays the same. - Assigning a value of another type, such as quotes around
34.
Key takeaways
name = valuereplaces what avarholds.- Assignment copies a value, and later changes to the source do not affect the copy.
- A reassignment has no
varand must use the variable's existing type.
