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

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

    The main difference is that var variable value can change, and let can't. If you want to have a user input data, you would use var so the value can be changed and use let datatype variable so the value can not be changed.

    var str      = "dog"  // str value is "dog"
    str          = "cat"  // str value is now "cat"
    
    let strAnimal = "dog" // strAnimal value is "dog"
    strAnimal     = "cat" // Error !
    

提交回复
热议问题