15

Consider this two codes:

if let myValue = myObject.value as NSString?{
 //logic here
}

vs

if var myValue = myObject.value as NSString?{
 //logic here
}

I know the let keyword is define a constant, is this mean that the first line of code, if the myObject.value is NSString , the myValue constant will be made? This looks confusing.

DNB5brims
  • 29,344
  • 50
  • 131
  • 195
  • 8
    I would argue this is not a duplicate of [What is the difference between `let` and `var` in swift?](http://stackoverflow.com/questions/24002092/what-is-the-difference-between-let-and-var-in-swift) – Krzak May 29 '15 at 13:48
  • 4
    This is not a duplicate of the post "What is the difference between `let` and `var`?" The `if let` and `if var` optional binding are different – Duncan C Mar 04 '16 at 18:56

5 Answers5

26

If you use the let then you will not be able to change myValue.

if let myValue = myObject.value as NSString? {
    myValue = "Something else" // <-- Compiler error
}

On the other hand with var you can.

if var myValue = myObject.value as NSString? {
    myValue = "Something else" // <-- It's fine
}

Please note that myValue does exists only within the scope of the if and changing its value does not produce effect outside of its scope.

Hope this helps.

Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
  • 1
    Your (almost) last sentence is wrong. Check http://swiftstub.com/774222102/ – Krzak May 29 '15 at 12:23
  • 1
    In your example you are not changing the value of `test`. You are changing a value `referenced` by `test`. It's different. I think my sentence is correct: if you change the value of `myValue` you do not produce effects outside of its scope. – Luca Angeletti May 29 '15 at 12:26
  • Anyway you made a smart observation. I am going to update my answer in order to include your example. – Luca Angeletti May 29 '15 at 12:34
  • Some types are `reference types` and those will always refer to another value. Other's are `value types` and for those you are right. – Krzak May 29 '15 at 13:46
  • 1
    Yes in Swift we have `Reference Type` when we use a `Class` and `Value Type` when we use a `Struct`, an `Enum` or a `Tuple`. But please note that this does not change my answer. `myValue` can be a Class, a Struct, an Enum or a Typle. If I change its value inside the `IF` (I mean I write `myValue = something`) the effect does not propagate outside the IF. – Luca Angeletti May 29 '15 at 14:03
  • 1
    It's worth pointing out that `if let myValue = ...` will not allow you to change the referenced object through `myValue`. That is, inside the block, `myValue.someProperty = aValue` will generate a compiler error, just as trying to assign to `myValue` itself. – Ted Hopp May 04 '18 at 19:49
7

You use let to create a constant, so in the first example you cannot change myValue.

When you use var, myValue is a variable which you can change inside the if statement.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Greg
  • 25,317
  • 6
  • 53
  • 62
0

The variable type is a different depending if you are using let or var.

let is final and can not be changed afterwards. var is a normal variable.

Your myValue cannot be changed or modified if you using let in this statement.

Sven Liebig
  • 828
  • 7
  • 18
0

The example you stated is of Optional Binding

Optional binding is used to determine whether an optional has a value, and if so, to make that value available as a temporary constant or variable.

if let myValue = myObject.value as NSString?{
 //logic here
}

Above code will extract myObject.value if exist as a constant, whereas the code below will extract myObject.value as a variable

if var myValue = myObject.value as NSString?{
 //logic here
}
Vivek Molkar
  • 3,910
  • 1
  • 34
  • 46
0

“Use let to make a constant and var to make a variable. The value of a constant doesn’t need to be known at compile time, but you must assign it a value exactly once.

This means you can use constants to name a value that you determine once but use in many places.

 var myVariable = 42
 myVariable = 50
 let myConstant = 42

A constant or variable must have the same type as the value you want to assign to it.

However, you don’t always have to write the type explicitly. Providing a value when you create a constant or variable lets the compiler infer its type.

In the example above, the compiler infers that myVariable is an integer because its initial value is an integer.

Read the : iBook The Swift Programming Language

Thanks .

Shanmugasundharam
  • 2,082
  • 23
  • 32