Data and types
Data is anything meaningful to the computer: a number, a word, a yes-or-no answer. Swift groups data into types. The ones you will meet first are:
Int, for whole numbers such as3and-12;Double, for numbers with a fractional part such as3.14;String, for text such as"John Doe";Character, for a single character such as"a"or"?";Bool, fortrueorfalse.
Types matter because they decide what you can do with a value. You can add two numbers, but 12 the number and "12" the text are different things, and Swift will not mix them silently. A Character is written with double quotes like a string, and you ask for it with a type annotation: let initial: Character = "M". Single quotes are not used for text in Swift.
Variables and constants
A variable is a name attached to a value stored in memory. You can picture a box with a label: the value is inside, the name is on the outside. Anywhere in your program, you use the name to get the value back.
Swift gives you two ways to declare one:
letcreates a constant, whose value is fixed once it is set.varcreates a variable, which you can reassign later.
Use let by default, and switch to var only for values that really change. The compiler helps by warning about a var that is never changed.
Declaring and initialising
A declaration has the keyword, the name, an optional type annotation after a colon, and an initial value after =:
let name: String = "AlgoCademy"
var age: Int = 10
The name comes first and the type follows it. You can usually leave the annotation out, and Swift infers the type from the value:
let pi = 3.14
let inferred = 42
print(type(of: pi), type(of: inferred))
Double Int
Using a variable
Write the name wherever you want the value, for example inside print:
var name = "AlgoCademy"
var age = 10
print(name)
print(age)
AlgoCademy
10
To put a variable inside text, use string interpolation:
print("\(name) is \(age)")
AlgoCademy is 10
Reassigning
To change a var, assign again with = and without the keyword. The variable already exists, so writing var again would declare it a second time and cause an error:
var age = 30
age = 31
age += 1
print(age)
32
What the compiler rejects
- Reassigning a constant. After
let name: String = "A", the linename = "B"fails with an error that says you cannot assign tonamebecause it is aletconstant. - Wrong type. After
var age: Int = 30, the lineage = "31"fails withcannot assign value of type 'String' to type 'Int'. The same happens forage = 31.5, which is aDouble. - Using a variable before it has a value.
var x: Intfollowed byprint(x)is an error: the variable is used before being initialised. Swift allows a declaration without a value only if every path assigns it before it is read, so give values at declaration whenever you can. - Declaring the same name twice. A second
let x = 6in the same scope is an invalid redeclaration. - Case. Names are case-sensitive:
ageandAgeare different, and a misspelling makes the compiler say it cannot find the name in scope.
The exercise
The exercise has three steps, one line each:
- Declare the constant
namewithlet, annotate it asStringand set it to"Michael". - Declare the variable
agewithvar, annotate it asIntand set it to the number30, with no quotes. - Reassign
ageto31, with novarin front.
let name: String = "Michael"
var age: Int = 30
age = 31
Key takeaways
- A variable is a name for a value.
letmakes a constant andvarmakes something you can change.- The type annotation follows the name after a colon, and it is optional when the value makes the type clear.
- Every value has a type, and Swift will not assign a value of one type to a variable of another.
- Give a variable its value when you declare it.
