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

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

    let is used to define constants and var to define variables. You define the string using var then particular String can be modified (or mutated) by assigning it to a variable (in which case it can be modified), and if you define the string using let its a constant (in which case it cannot be modified):

    var variableString = "Apple"
    variableString += " and Banana"
    // variableString is now "Apple and Banana"
    
    let constantString = "Apple"
    constantString += " and another Banana"
    // this reports a compile-time error - a constant string cannot be modified
    

提交回复
热议问题