What’s the difference between var and let? Which one would you choose for properties in a struct and Class why?
Let is an immutable variable, meaning that it cannot be changed, other languages call this a constant.
Var is a mutable variable, meaning that it can be changed.
“Use let to make a constant and var to make a variable”
let aPerson = Person(name:Foo, first:Bar)
//< data of aPerson are changeable, not the reference
var aPerson = Person(name:Foo, first:Bar)
//< both reference and data are changeable.var aPersonA = Person(name:A, first: a)
var aPersonB = Person(name:B, first: b)
aPersonA = aPersonB
//aPersonA now refers to Person(name:B, first: b)Value and Reference Type
Reference Type(Class)
Swift's classes are mutable a-priory
var + class
It can be reassigned or changed
let + class = constant of address
It can not be reassigned and can be changed
Value(Struct, Enum)
Swift's struct can change their mutability status:
var + struct = mutable
It can be reassigned or changed
let + struct = *immutable = constant of value
It can not be reassigned or changed
*immutable - check testStructMutability test
Value typeis a type whose value is copied when it’s assigned to a variable or constant, when it’s passed to a function or when it's returned from function. (Alsoasandischecks make a copy of struct)
Struct,Enum,Tuplestruct String,struct Array(Set,Dictionary)
- When you assign or pass
value typea new copy of data is created. Actually thecopy on write-COWmechanism is used with some optimisations, for example the copy is created when object is modified - When you modify an instance it has only local effect.
- The Stack Memory is used.
Reference types are not copied when they are assigned to a variable or constant, or when they are passed to a function
Class,Function
- When you assign or pass
reference typea new reference to original instance will be created(the address of instance is copied). - When you modify an instance it has a global effect because the instance is shared and accessible by any reference that points on it.
- The Heap Memory is used.
Value type is recommended to use by default. The biggest advantage of Value type is that usually they are thread safe
Reference type Pros:
- they can be inherited,
deinit()can be used,- compare instances by reference
===, Objective-Cinteroperability becauseValue Typewas introduced in Swift.
- Structs are much safer and bug-free, especially in a multithreaded environment. Swift value types are kept in the stack. In a process, each thread has its own stack space, so no other thread will be able to access your value type directly. Hence no race conditions, locks, deadlocks or any related thread synchronization complexity.
- Class does support Inheritance. Class is a reference type and is stored in the heap part of memory which makes a class comparatively slower than a struct in terms of performance. Unlike a class, a struct is created on the stack. So, it is faster to instantiate (and destroy) a struct than a class. Unless struct is a class member in which case it is allocated in heap, along with everything else.
- Value types do not need dynamic memory allocation or reference counting, both of which are expensive operations. At the same time methods on value types are dispatched statically. These create a huge advantage in favor of value types in terms of performance.
Mutating Struct's Functions

Comments
Post a Comment