What is the difference between `let` and `var` in

2019-01-01 01:58发布

问题:

What is the difference between let and var in Apple\'s Swift language?

In my understanding, it is a compiled language but it does not check the type at compile time. It makes me confused. How does the compiler know about the type error? If the compiler doesn\'t check the type, isn\'t it a problem with production environment?

回答1:

The let keyword defines a constant:

let theAnswer = 42

The theAnswer cannot be changed afterwards. This is why anything weak can\'t be written using let. They need to change during runtime and you must be using var instead.

The var defines an ordinary variable.

What is interesting:

The value of a constant doesn’t need to be known at compile time, but you must assign the value exactly once.

Another strange feature:

You can use almost any character you like for constant and variable names, including Unicode characters:

let               
                            
标签: swift