What's the difference between let and var?

Swift lets you create both variables and constants as ways to reference your data, but there's a strong push (even Xcode warnings!) if you create things as variables then never change them. To make a constant, use let like this:

let 10

To make a variable, use var like this:

var 20

The reason Swift strongly encourages you to use constants wherever possible is because it's safer: if you say "this value will never change," then Swift will refuse to let you change it even by accident. It also opens the possibility of compiler optimizations if the system knows certain data will not change.

Comments

Popular posts from this blog

In Swift, Optionals and Optional Binding are key concepts for handling values ?

What is benifit of using higher order functions?

Swift Optionals and force unwrapping?