This article mainly introduces the value types in swift and the use of reference types, this article explains the difference between value type and reference type, how to choose the type, when to use the value type, when the reference type and so on, the need for friends can refer to the
The types within Swift are divided into two categories:
Value Types: each instance retains a unique copy of the data, typically in the form of a struct (struct), an enumeration (enum), or a tuple (tuple).
Reference type (Reference type): each instance shares the same data source, typically in the form of classes (class).
In this blog post, we will describe the advantages of each of the two types, and how to choose to use them.
Difference between a value type and a reference type
The most basic difference between a value type and a reference type is the result of copying. When a value type is replicated, it is equivalent to creating a completely independent instance that retains its own unique data that is unaffected by the data changes of other instances:
The code is as follows:
The following is an example of a value type
struct S {var data:int =-1}
var a = S ()
var B = A//b is a copy of a
A.data = 42//change data for a, not affected by B
println ("(A.data), (b.data)")//Output result "42,-1"
Value type like a photocopy of the ID card, the contents of the copy will not change after the copy is made and the contents of the original are modified.
On the other hand, when you copy a reference type, you actually create a shared instance of the replica silently, both of which share a set of data. So modifying the data of any one of these instances will also affect the other.
The code is as follows:
The following is an example of a reference type
Class C {var data:int =-1}
var x = C ()
var y = x//y is a copy of X
X.data = 42//change x's data, equal to modify Y at the same time
println ("(X.data), (y.data)")//Output result "42, 42"
The role of Mutation (modification) in security
Value types are more likely to make it easier to sort out the situation in a lot of code than a reference type. If you always get an independent copy of the instance, you can be assured that it will not be silently modified by other parts of your app. This is especially important in a multithreaded environment, because another thread may be secretly modifying your data. This can result in serious program errors, which are very difficult to eliminate during debugging.
Because the difference is primarily the result of modifying the data, when the data for the instance is read-only and there is no need to change, there is no difference in which type.
You may be thinking, sometimes I may also need a completely unchanged class. This makes it easier to use cocoa NSObject objects and preserves the benefits of value semantics. Today, you can use Swift to write an immutable class (immutable classes) by using only immutable storage attributes and bypassing any API that can modify the state. In fact, many of the basic cocoa classes, such as Nsurl, are designed to be immutable classes. However, the swift language currently only enforces the immutability of value types such as struct and enum, and does not have a reference type for a class. (for example, it is not supported to force the restriction of subclasses to immutable classes)
How do I choose a type?
So when we want to create a new type, how do we decide to use a value type or a reference type? When you use the Cocoa Framework, many APIs are used by NSObject subclasses, so you must use the reference type class. In other cases, there are several guidelines:
When to use the value type:
When you want to compare the data of an instance with the = = operator
If you want the copy of that instance to remain independent,
When data is used by multiple threads
When to use the reference type (Class):
To compare instance identities with the = = operator
You want to have the time to create a shared, mutable object
Within Swift, arrays (array), strings (string), dictionaries (Dictionary) are all value types. They are like the C language inside the simple int value, is a separate data individual. You don't have to take any effort to prevent other code from secretly modifying them. More importantly, you can pass variables safely between threads without having to sync them. In the spirit of Swift's high security, this pattern will help you write more manageable code with Swift.