Value and reference types in Swift -A deep dive
The value and reference types are a very important concept in Swift. A value type contains the data within its own memory. A reference type holds a pointer to another memory location that holds the actual data. In this article, I will explain: The basics of value and reference types Differences between value and reference types Why does Apple prefer to use value types by default? When to use value types or reference types The basics of value and reference types A Value type holds its contents in memory allocated on the stack. When you create a Value type, one single space in memory is allocated to store the value. Here, the variable directly holds a value. Let’s assume, you assign it to another variable. The value is copied directly and both variables work independently. But, their addresses are different. A reference type holds a memory address to the object but not the actual object itself. It represents the address of the variable rather than the data itself. Assigning a reference variable to another variable doesn’t copy the data. Instead, it creates a second copy of the reference, which refers to the same address as the original value. Unlike value…