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

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

    let keyword defines a constant

    let myNum = 7
    

    so myNum can't be changed afterwards;

    But var defines an ordinary variable.

    The value of a constant doesn’t need to be known at compile time, but you must assign it a value exactly once.

    You can use almost any character you like for constant and variable names, including Unicode characters;

    e.g.

    var x = 7 // here x is instantiated with 7 
    x = 99 // now x is 99 it means it has been changed.
    

    But if we take let then...

    let x = 7 // here also x is instantiated with 7 
    x = 99 // this will a compile time error
    
    0 讨论(0)
  • 2020-11-22 11:41

    Declare constants with the let keyword and variables with the var keyword.

    let maximumNumberOfLoginAttempts = 10 var currentLoginAttempt = 0   
    let maximumNumberOfLoginAttempts = 10
    var currentLoginAttempt = 0
    

    Declare multiple constants or multiple variables on a single line, separated by commas:

    var x = 0.0, y = 0.0, z = 0.0
    

    Printing Constants and Variables

    You can print the current value of a constant or variable with the println function:

    println(friendlyWelcome)
    

    Swift uses string interpolation to include the name of a constant or variable as a placeholder in a longer string

    Wrap the name in parentheses and escape it with a backslash before the opening parenthesis:

    println("The current value of friendlyWelcome is \(friendlyWelcome)")
    

    Reference : http://iosswift.com.au/?p=17

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

    var is the only way to create a variables in swift. var doesn't mean dynamic variable as in the case of interpreted languages like javascript. For example,

    var name = "Bob"
    

    In this case, the type of variable name is inferred that name is of type String, we can also create variables by explicitly defining type, for example

    var age:Int = 20
    

    Now if you assign a string to age, then the compiler gives the error.

    let is used to declare constants. For example

    let city = "Kathmandu"
    

    Or we can also do,

    let city:String = "Kathmandu"
    

    If you try to change the value of city, it gives error at compile time.

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

    SIMPLE DIFFERENCE

    let = (can not be changed)

    var = (any time update)

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • “Use let to make a constant and var to make a variable”

    Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l

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