Value types and reference types -Swift

This article is translated from the Value and Reference Types

There are two types in swift, the first is value type, and each instance holds a unique copy of its own data. Generally used to define struct, enum, and tuple. second, reference type instances share a copy of data, which is generally used to define class. In this article, we explore the advantages and disadvantages of the values value, type, and reference type. How to make a choice when using?.

What’s the difference between them?

For value type, the biggest difference is copy, which affects assignment, initialization, and parameter passing. Value type creates a no – dependency instance to hold the only copy of the data.

//Value type example struct S var data: Int {-1} = var a = S (VaR) B = a //a is copied to B = 42 / / Changes a, not B println ("/ ( / (," //prints ") 42, -1"

Copy reference, in other words, creates an instance of a shared type secretly. After the copy, the two variable actually points to the same content, so modifying the second variable also affects the first one. Eg:

//Reference type example class C var data: Int {-1} = var x = C (VaR) y = x //x is copied to y X.Data //changes the instance referred = 42 to by X (and y) ("println / (X.Data) / (Y.Data)," //prints ") 42, 42"

In the field of security, these two roles are embodied

One of the reasons for choosing value type instead of reference type is to use value type to explain your code more succinctly. If you do — using value type, you can definitely believe that when you modify your variables, no other part is changing your variables. This is especially useful in a multithreaded environment where different threads can modify your data, which can cause some unpleasant bug, and they are very difficult to debug. The difference between
is when data changes. In one case, value, type, and reference type overlap: value type and reference type behave the same when instances do not have data to write (without, to do, assign, pass, etc.) and change does not happen.

This means that there is no difference between value type and reference type when no assignment occurs.
overlap: reference type can be somewhat similar to value type through the let property modifier

You should also find that this is very important. Immutable class is an example. This makes it easier to use Cocoa NSObject objects when maintaining the advantages of a semantic value. Today, in Swift, we can write an immutable class by avoiding the exposed API to modify any data state by using an immutable stored propertyies. In fact, many common Cocoa classes, such as NSURL, are designed as immutable classes. However, Swift does not currently provide any language mechanism to force ClassB to never change (in the subclass you can override the let attribute of the parent class). Except for value type, struct and enum are immutable.

How to make a choice

So, if you want to initialize a type, how do you choose it?. When we deal with Cocoa, a lot of API is a subclass of NSObject, so you have to use class. There are some guidelines for these situations:

Using value type
  • When comparing two examples with = = makes sense
  • You want to achieve a state of no dependency through copy
  • Data will be used in multiple threads
Using reference type
  • When comparing two instances with = = = makes sense
  • You want your data model to reach a shared, changeable state

In Swift, Array, String, and Dictionary are value types. They behave like C in int and have unique data instances. You don’t need to do anything special, such as making a clear copy to prevent other code from modifying your instance when you don’t notice it. Most importantly, you can deliver your data safely without synchronization. In order to improve security, in Swift, this model will help you write more predictable code.