Variable Types

1) Constants

let myConstant: Int = 0

This is, of course, not a variable. This is a constant. One of the nice things about Swift constants is that

let decideLater: Bool

is perfectly legitimate, as long as it is set before initialisation has ended: Combined with

init(somethingElse: String){
if somethingElse = "You'll never guess" {
decideLater = true
} else {
decideLater = false

it’s possible to not only defer initialisation of a let constant, but to involve calculations depending on the rest of your application.

2) Variables

var myVariable: Int = 3

You can change this at any point in time: in an initialiser, and while the application is running. (OK, changing var declared in structs is a bit more involved, but the principle is sound.)

3) Computed Properties

var privateValue: Int = 15

var calculatedValue: Int {
return privateValue * 2

Here, the calculation depends on other factors. A typical use case is to set the height and width of a rectangle, and calculate its area. Computed properties are read-only, and are calculated every time they are accessed.

4) [Lazy] Closures

lazy var closureVar: Int = {
let myExpensiveValue = //insert expensive calculation
return myExpensiveValue

This value gets calculated exactly once, during initialisation. If you prefix it with ‘lazy’, the initialisation happens only when the value is retrieved – if nobody ever calls this, it will never be calculated, making it ideal for expensive setup calls. Afterwards, this variable behaves like any ordinary variable: you can assign any other value to it.