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

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

    let is a constant value, so it can never be changed.

    let number = 5  
    number = 6               //This will not compile.
    

    Var is a variable, and can change (but after it is defined not to a different data type.)

    var number = 5
    number = 6               //This will compile.
    

    If you try changing the variable to a different dataType, it will not work

    var number = 5
    number = "Hello World"   //This will not compile.
    

提交回复
热议问题