HomeSearch

Swift Var Versus Let Keywords

This Swift tutorial explains the difference between var and let. It covers the cannot assign to value error.
Var. X is 10. The identifier "x" has a binding to the value 10. As a var, it can be changed to equal 20. As a constant (specified with let) it can never be changed.
With let, we create immutable things (constants) in our Swift code. Generally constants are more durable and understandable in code. But for loops and objects like arrays, var is needed.
Var example. Let us use the var keyword in a simple example. Here we bind the identifier "color" to the string "blue." We then bind "color" to green with a reassignment.

Next: After this example, we try to rewrite the code with let. We use 2 let constants instead of 1 var.

Swift program that uses var // Create a variable and assign a string literal to it. var color = "blue" print(color) // Change the assigned value. color = "green" print(color) Output blue green
Let error. Now we cause some trouble. We create a constant with the value "blue." But then we try to change its binding to point to "green." This fails and a compile-time error is caused.
Swift program that uses let, causes error // This part works. let color = "blue" print(color) // But this part does not compile. // ... A constant cannot be reassigned. color = "green" print(color) Output Cannot assign to value: 'color' is a 'let' constant
Separate let constants. Here we fix our "cannot assign to value" error. We use two "let" constants. This style of code may lead to better, more durable programs.

Tip: When possible, this style of code is ideal. It means no mistaken assignments to color1 or color2 can occur.

Swift program that uses let, separate bindings // For constants, we must use separate bindings. let color1 = "orange" print(color1) let color2 = "mauve" print(color2) Output orange mauve
Var argument. A func cannot modify its arguments in Swift (by default). With the var keyword, we specify that an argument can be modified by the method's statements.
Swift program that uses var argument func incrementId(var id: Int) -> Int { // Print and increment the var argument. print(id) id++ print(id) return id } var id = 10 // Call incrementId with argument of 10. // ... Print the returned value. print(incrementId(id)) Output 10 11 11
Mutating operator error. Here the incrementId func tries to increment its argument Int. But the argument is not marked with var, so this causes a "mutating operator" error.
Swift program that causes mutating operator error func incrementId(id: Int) { // This causes an error. id++ } var id = 55 incrementId(id) Output Cannot pass immutable value to mutating operator: 'id' is a 'let' constant
Typically, the let-keyword is best when it can be applied. If a variable is mutated in a program, the var keyword can be used. But immutable things offer advantages and are often preferred.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls