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

后端 未结 30 1489
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 11:09

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

30条回答
  •  甜味超标
    2020-11-22 11:41

    let keyword defines a constant

    let myNum = 7
    

    so myNum can't be changed afterwards;

    But var defines an ordinary variable.

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

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

    e.g.

    var x = 7 // here x is instantiated with 7 
    x = 99 // now x is 99 it means it has been changed.
    

    But if we take let then...

    let x = 7 // here also x is instantiated with 7 
    x = 99 // this will a compile time error
    

提交回复
热议问题