Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How exactly does the "let" keyword work in Swift?

I've read this simple explanation in the guide:

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

But I want a little more detail than this. If the constant references an object, can I still modify its properties? If it references a collection, can I add or remove elements from it? I come from a C# background; is it similar to how readonly works (apart from being able to use it in method bodies), and if it's not, how is it different?

like image 258
Max Yankov Avatar asked Jun 02 '14 20:06

Max Yankov


People also ask

What does let stand for in Swift?

Swift employs the keywords let and var for naming variables. The let keyword declares a constant, meaning that it cannot be re-assigned after it's been created (though its variable properties can be altered later). The var keyword declares a new variable, meaning that the value it holds can be changed at a later time.

What is difference between LET and VAR in Swift?

let is used to declare an immutable constant. You cannot change the value of it later. var is used to create a mutable variable that you can change later.

What is difference between static let and static VAR in Swift?

You create static variable by appending static keyword in front of your variable declaration. We will be using playground to explore more. When we define any variable as let, it means it's values cannot be modified, On the other hand if we define any variable as var it means it's values can be modified.

What is use of static let in Swift?

In Swift, Static let and Static var are considered as Type Properties, which means that they can be accessed by their type. In this example, two properties have static keyword, one is constant and another one is variable.


2 Answers

let is a little bit like a const pointer in C. If you reference an object with a let, you can change the object's properties or call methods on it, but you cannot assign a different object to that identifier.

let also has implications for collections and non-object types. If you reference a struct with a let, you cannot change its properties or call any of its mutating func methods.

Using let/var with collections works much like mutable/immutable Foundation collections: If you assign an array to a let, you can't change its contents. If you reference a dictionary with let, you can't add/remove key/value pairs or assign a new value for a key — it's truly immutable. If you want to assign to subscripts in, append to, or otherwise mutate an array or dictionary, you must declare it with var.

(Prior to Xcode 6 beta 3, Swift arrays had a weird mix of value and reference semantics, and were partially mutable when assigned to a let -- that's gone now.)

like image 161
rickster Avatar answered Nov 02 '22 05:11

rickster


Let


Swift uses two basic techniques to store values for a programmer to access by using a name: let and var. Use let if you're never going to change the value associated with that name. Use var if you expect for that name to refer to a changing set of values.

let a = 5  // This is now a constant. "a" can never be changed. var b = 2  // This is now a variable. Change "b" when you like. 

The value that a constant refers to can never be changed, however the thing that a constant refers to can change if it is an instance of a class.

let a = 5 let b = someClass() a = 6  // Nope. b = someOtherClass()  // Nope. b.setCookies( newNumberOfCookies: 5 )  // Ok, sure. 

Let and Collections


When you assign an array to a constant, elements can no longer be added or removed from that array. However, the value of any of that array's elements may still be changed.

let a = [1, 2, 3] a.append(4)  // This is NOT OK. You may not add a new value. a[0] = 0     // This is OK. You can change an existing value. 

A dictionary assigned to a constant can not be changed in any way.

let a = [1: "Awesome", 2: "Not Awesome"] a[3] = "Bogus"             // This is NOT OK. You may not add new key:value pairs. a[1] = "Totally Awesome"   // This is NOT OK. You may not change a value. 

That is my understanding of this topic. Please correct me where needed. Excuse me if the question is already answered, I am doing this in part to help myself learn.

like image 28
StaticReturn Avatar answered Nov 02 '22 05:11

StaticReturn