Value types vs reference types: The main differences in the Swift programming language

Continuing the series of articles about Swift programming language, today, Arrowhitech gives you the article about Swift Types, namely Value Types vs Reference Types. Let’s take a look with us right away! 

What are the Value types vs reference types? 

As you probably know, each variable, constant, as well as property in Swift all has a type. Then, the type of an integer number is Int, the type of a string of text is String, and so much more. For example, they allow you to set a value of a specific type to a constant. So, let’s take a look at following code:

let age:Int = 42

As you see, the value 42 of type Int is allocated to a constant called age in the example above. Besides, the type Int – an integer – is assigned to the constant. The Int type is a struct in and of itself.

For more details, many things such as: enums, structs, optionals, closures, tuples, classes, and function types are among the various kinds that can be distinguished. In summary, we save things in programming, and those objects have distinct names and kinds to make them easier to distinguish.

What are Value Types?

Coming to the first type in this topic is Value Types. Then, let’s have a glance at the following instance to know the way they work:

Code of Value Types

var a = 99

var b = a

b += 1

print(a) // 99

print(b) // 100

In fact, below are the exactly what Value Types do:

  • Firstly, the constant has the value 99. This constant’s type is Int, which is an integer number and a value type.
  • The value of an is then allocated to a new constant called b. The value is replicated, and a and b now each have their own copy of the number 99. (This is critical!)
  • Then, the constant b multiplies with one, giving it the value 100. The constant a remains at 99.
  • At the end, print out both values.

In particular, what’s crucial here is that when you assign a to b, the value of constant an is copied to constant b. As a result, it creates a second copy of the value 99.

This claim is supported that when 1 is added to b, the value of a remains unchanged (line 3). We can observe that a and b are distinct and distinct values. Assigning a to b (line 2) makes a duplicate of the data. This is how you know you’re dealing with a value type!

Value types vs reference types

The following kinds are value types in Swift:

  • Integer, String, Double, and Bool are examples of structures.
  • Arrays, dictionaries, and sets are all types of data structures.
  • Tuples and enumerations

For a variety of reasons, the Swift programming language prefers value types to reference types. When value types are the default, it’s easier to reason about code. Because a value type can’t be altered by another thread or process while you’re working with it, your code is less prone to errors.

What are reference types? 

In fact, reference types work very differently compared to Value types. Specifically, when a reference type is assigned to a new variable, constant, or property, it preserves a reference to a shared instance.

Code of reference types

class Car {

    var speed = 0

}

let racecar = Car()

racecar.speed = 250

let bus = racecar

bus.speed = 40

print(racecar.speed) // 40

print(bus.speed) // 40

Do you notice the magic? As you can see, racecar.speed and bus.speed both set to 40. Then, what’s going on in the code is as follows:

  • At the top, we define a Car class with a single speed property of type Int. It’s set to 0 by default.
  • Then we’ll make a Car instance and assign it to the constant racecar. Then, it set the racecar’s speed property to 250.
  • After that, we’ll assign racecar to a new constant known as bus. We also changed the bus’s speed attribute to 40. 
  • At the end, print the racecar and bus speed properties.

In reality, Car type is a class so it is a reference type. When reference types are assigned to a new variable, constant, or property, they aren’t replicated like value types are. Instead, it will transfer the instance’s reference. Both constants point to the same instance because this reference points to the same instance. Both constants have the same instance, you could say.

Value types vs reference types

Coming to Swift, there are some types that are reference types:

  • Classes
  • Closures
  • Functions

This is evident in the preceding case. To begin, the Car class is a subset of the Car type. The Car instance is not replicated when a racecar is assigned to a bus. Instead, copy the Car instance’s reference to bus. The terms racecar and bus now refer to the same thing! As a result, if you modify one, the other will alter as well…

The main differences between Value types vs reference types

Value types and reference types

#1. Firstly, Value type: each instance preserves a copy of its data that is unique to it. When an instance is assigned to a variable, constant, or property, a copy of it is created.

#2. Secondly, Reference type: Each instance of a reference type shares the same copy of the data. When a variable, constant, or property is assigned to a single instance, a reference to that instance is formed.

In general, it is very simple to remeber the difference if you think of the word “reference”.

Mutability With “var” and “let” 

First and foremost, you’ll easily notice something strange in the previous case if you look attentively. Besides, take a note that racecar is a constant. What’s more, Constants are unchanging. 

In addition, for value types and reference types, let and var behave differently. The gist of it is as follows:

#1. Reference types

When it comes to reference types, the reference must remain consistent. So you can modify the instance, such as its characteristics, but not the reference.

#2. Value types

The value of a value type must be constant at all times. As a result, you are unable to alter the value or any of its properties. Not only that, the value is unchangeable.

All in all, you can deduce from this that controlling mutability is significantly easier using let and value types.

Also, even when a reference type is specified with let, you can always change its properties. Because the immutability of a reference type is dependent on how you implement it, this makes it more difficult to reason about your code.

In conclusion

Although all are Types, Value Types vs Reference Types have many different points. Above all, ArrowHiTech hopes you will take a lot of benefits from this blog above. In addition to this topic, we also review many other features of Swift programming language. Some of them are: Swift function, Swift init, Swift generics and so on. If you are getting any trouble when working with Swift, let us know immediately. With many years of experience in  Swift and Objective-C IOS App Development Services, we are confident that we can resolve any problem for you. 

Tags

Share