2

As Apple Documentation says:

Use let to make a constant and var to make a variable. The value of a constant doesn’t need to be known at compile time, but you must assign it a value exactly once. This means you can use constants to name a value that you determine once but use in many places.

Let's consider some class:

class A {
    var a = [String]()
}

Since array a is mutable, it's defined via var. But what if we consider class B, where instance of A is property?

class B {
    let b = A()
}

Even if b is mutable, let keyword will be ok, because reference won't be changed. On the other hand, var will be ok too, because content of b can be changed. What should I pick in this example - let or var?

Arsen
  • 43
  • 2
  • 5
  • 1
    you can always use `var` where you can use `let`. `let` only tells the rest of your program that this element cannot be re-assigned – njzk2 Feb 06 '15 at 20:28
  • 1
    Did you see [What is the difference between `let` and `var` in swift?](http://stackoverflow.com/questions/24002092/what-is-the-difference-between-let-and-var-in-swift) ? – Martin R Feb 06 '15 at 20:30
  • 2
    I usually just bend over backwards to have everything as let. immutable > mutable and even though it doesn't matter in your second example, I put let there to remind me of that. – Andreas Du Rietz Feb 06 '15 at 20:33

4 Answers4

10

Use let whenever you can. Use var when you must. Making things immutable makes a lot of bugs impossible, so it should be your default choice. As much as possible, set all your values in init and never change them. Similarly, you should use struct when you can, and class when you must (though in my experience this is harder to achieve than using let).

So in your example, if you cannot set A.a during initialization, then yes, it should be var. But there is no need in your example to use var for B.b. (And there's no reason in either example to use class, at least in the way you've presented the question.)

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
2

Let's give these better names to help with our reasoning. Let's say

class Head {
    var hairs = [String]()
}

class Person {
    let head = Head()
}

In this example, a Person has exactly one head, and for the lifetime of each Person, his/her head will always been that same head. However, that head's hairs can grow in, fall out, or otherwise change. Person's ownership of this head has no bearing on the Head's relationship to its hairs.

As Rob mentioned, you should always use let unless you have a good reason not to. Immutability is your friend when it comes to reasoning about program behavior.

oltman
  • 1,772
  • 1
  • 14
  • 24
1

Use let when your object does not change its value after has been set a value. Use var if your object can change its value more than 1 time.

'let' is for constance. 'var' is for something variable.

However, constant restriction is only applied to the object but not its attributes if the object is an instance of class (value are passed by reference). Depending on type of those attributes (constant or variable), we can change their value after. This is not true for structure.

For example:

class VideoMode {
    var interlaced = false
    var frameRate = 0.0
    var name: String?
}

in a function, you declare

let vm = VideoMode()
print("starting framerate is \(vm.frameRate)") // -> print starting framerate is 0.0
vm.frameRate = 20.0
print("framerate now is \(vm.frameRate)")  // -> print framerate now is 20.0

//we can change .frameRate later to 10.0, there is no compile error
vm.frameRate = 10.0
print("framerate now is \(vm.frameRate)")  // -> print framerate now is 10.0
Vlad Khambir
  • 4,313
  • 1
  • 17
  • 25
Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44
0

When you declare a variable with var, it means it can be updated, it is variable, it’s value can be modified.

When you declare a variable with let, it means it cannot be updated, it is non variable, it’s value cannot be modified.

var a = 1 
print (a) // output 1
a = 2
print (a) // output 2

let b = 4
print (b) // output 4
b = 5 // error "Cannot assign to value: 'b' is a 'let' constant"

Let us understand above example: We have created a new variable “a” with “var keyword” and assigned the value “1”. When I print “a” I get output as 1. Then I assign 2 to “var a” i.e I’m modifying value of variable “a”. I can do it without getting compiler error because I declared it as var.

In the second scenario I created a new variable “b” with “let keyword” and assigned the value “4”. When I print “b” I got 4 as output. Then I try to assign 5 to “let b” i.e. I’m trying to modify the “let” variable and I get compile time error “Cannot assign to value: ‘b’ is a ‘let’ constant”.

Source: https://thenucleargeeks.com/2019/04/10/swift-let-vs-var/

Aditya Malviya
  • 1,907
  • 1
  • 20
  • 25