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

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

    According to The Swift Programming Language Book

    Like C, Swift uses variables to store and refer to values by an identifying name. Swift also makes extensive use of variables whose values cannot be changed. These are known as constants, and are much more powerful than constants in C.

    Both var and let are references, therefore let is a const reference. Using fundamental types doesn't really show how let is different than const. The difference comes when using it with class instances (reference types):

    class CTest
    {
        var str : String = ""
    }
    
    let letTest = CTest()
    letTest.str = "test" // OK
    
    letTest.str = "another test" // Still OK
    
    //letTest = CTest() // Error
    
    var varTest1 = CTest()
    var varTest2 = CTest()
    var varTest3 = CTest()
    
    varTest1.str = "var 1"
    varTest2.str = "var 2"
    varTest3 = varTest1
    varTest1.str = "var 3"
    
    varTest3.str // "var 3"
    
    0 讨论(0)
  • 2020-11-22 11:55

    Found a good answer hope it can help :) enter image description here

    0 讨论(0)
  • 2020-11-22 11:56

    One more difference, which I've encountered in other languages for Constants is : can't initialise the constant(let) for later , should initialise as you're about to declare the constant.

    For instance :

    let constantValue : Int // Compile error - let declarations require an initialiser expression
    

    Variable

    var variableValue : Int // No issues 
    
    0 讨论(0)
  • 2020-11-22 11:57

    A value can be reassigned in case of var

     //Variables
     var age = 42
     println(age) //Will print 42
     age = 90
     println(age) //Will Print 90
    

    ** the newAge constant cannot be reassigned to a new value. Trying to do so will give a compile time error**

    //Constants
    let newAge = 92 //Declaring a constant using let
    println(newAge) //Will print 92.
    
    0 讨论(0)
  • 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 
    
    0 讨论(0)
  • 2020-11-22 11:58

    let is used for constants that can’t be modified while var is an ordinary variable

    Example:

    let name = “Bob” Something like name = “Jim” will throw an error since a constant can’t be modified.

    0 讨论(0)
提交回复
热议问题