In Swift there are certain types called value types and others are called reference types.
Struct, String, Array, Dictionary, Set are values types
Classes, functions, closures are reference types.
Starting from String type, check the following code:
let city = "Glasgow"
var anotherCity = city
anotherCity = "Edenburgh"
print("Value of city: \(city)")
print("Value of anotherCity: \(anotherCity)")
Output is:
Value of city: Glasgow
Value of anotherCity: Edenburgh
Changing the value of anotherCity didn’t affect the value of variable city.
On contrary, let’s see the behavior of reference types. Starting from class reference type:
class Car {
var name: String = ""
}
let car1 = Car()
car.name = "Toyota Camery"
let car2 = car
car2.name = "Mercedes Benz"
print("Car1 name: \(car1.name)")
print("Car2 name: \(car2.name)")
Output is:
Car1 name: Mercedes Benz
Car2 name: Mercedes Benz