Difference between value type and a reference type in iOS swift?

This is one of the basics of every programming language. Most of us might have started programming with C language. If you still remember the function call by value and call by reference, you might get some idea about what this exactly is. let’s see what apple says about this.

  • Value Type
  • Reference Type

Very basic definition:

Value Type — each instance keeps a unique copy of its data. A type that creates a new instance (copy) when assigned to a variable or constant, or when passed to a function.

Gif source: Internet

Let’s do some coding

Consider the following playground code block:

reference types

Class is a reference type. Copying a reference, implicitly creates a shared instance. After a copy, two variables then refer to a single instance of the data, so modifying data in the second variable also affects the original.

Note: Class is a reference type. That means the variable of a class type does not store an actual instance, but a reference to a location in the memory(heap) that stores the instance.

Question: What happens if we change the var to let in the above code block?

Answer: Well, Nothing happens. Do the following and run the code:

let peterVilla = Home()
let johnVilla = peterVilla

What if johnVilla is a var??

If johnVilla was a var , then it is mutable and he can upgrade or change his home any number of times. Just have a look at the following code:

What will happen if Home is a struct???

Consider the following playground code block. Here Home is a struct.

let peterVilla = Home()let johnVilla = peterVillajohnVilla = Home()//error: cannot assign to value: ‘johnVilla’ is a ‘let’ constant

Note: SO, For Value types , if we want to reassign the object or change the variable inside the object, it should be declared as mutable (‘var’).

Note: When you are mutating a value type in swift, you are not actually mutating the value. You are mutating the variable holding the value.

That being said, struct is not the only value type and class is not the only reference type in swift. Here are some examples:

When do we choose a Value Type over a Reference Type?

Source: Apple Docs

Use a value type when:

  • Comparing instance data with == makes sense. A double equal operator (aka ==) compares values.
  • You want copies to have independent state.
  • The data will be used in code across multiple threads. So that you don’t have to worry about the data being changed from another thread.

Use a reference type (e.g. use a class) when:

  • Comparing instance identity with === makes sense. === checks if two objects are exactly identical, right down to the memory address that stores the data.
  • You want to create shared, mutable state.

How Reference and Value types are stored in memory ?

  • Value Type — Get Stored on Stack Memory.
  • Reference Type — Get Stored on Managed Heap Memory.

Differences between Stack and Heap!

As mentioned earlier, reference type instances are stored in heap and instances of a value type such as struct resides in a region of memory called stack. If the value type instance is part of a class instance, the value is stored in the heap along with the class instance.

In short: Reference types always goes to the Heap, whereas Value Types always go where they are declared.

Reference : QUORA and few other sites

If you enjoyed reading this post, please share and give some clapps so others can find it 👏👏👏👏👏 !!!!

You can follow me on Medium for fresh articles. Also, connect with me on LinkedIn and Twitter.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store