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

后端 未结 30 1338
隐瞒了意图╮
隐瞒了意图╮ 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:58

    The keyword var is used to define a variable whose value you can easily change like this:

    var no1 = 1 // declaring the variable 
    no1 = 2 // changing the value since it is defined as a variable not a constant
    

    However, the let keyword is only to create a constant used when you do not want to change the value of the constant again. If you were to try changing the value of the constant, you will get an error:

    let no2 = 5 // declaring no2 as a constant
    no2 = 8 // this will give an error as you cannot change the value of a constant 
    

提交回复
热议问题