In swift, types fall into two categories: the first is a value type, each instance of that type holds a copy of the data, and the copy is unique to each instance, such as struct (struct), enum (enum), tuple (tuple), which are value types. The second is a reference type, where an instance of that type shares a single copy of the data (at the native level, that is, each instance of the type points to the same address in memory), such as the class is the reference type. In this article, we'll delve into the use of value types and reference types, and how to choose the right type in a scenario. Recommended LearningiOS Development first upgrade-swift languageVideo tutorials.
What's the difference between them?
The most basic feature of a value type is replication, which affects its assignment, initialization, and transfer of parameters. Take a look at the following code example:
Value type Example
struct S {var data:int =-1}
var a = S ()
var B = A//a copies a copy and assigns the copy to B
The data for a.data =//A has changed, but B hasn't changed.
println ("\ (a.data), \ (b.data)")//Prints "42, 1"
The copy behavior of the reference type is actually implicitly creating a shared instance as a reference to the original type. In the following example, two variables refer to the data of the unique shared instance, and the data of either of the two variables is changed to the same effect on the data of the original type:
Example of reference type
Class C {var data:int =-1}
var x = C ()
var y = x//assign X to Y
X.data = 42//Modify the data of X, actually modify the reference data, then the data of Y will also change
println ("\ (x.data), \ (y.data)")//Prints "42, 42"
The role of mutation in security
One important reason to choose a value type is to make it easier for you to understand and control your code. If you use value types, then all are unique data values, type copies are dealing with you, and your changes to the data are only used for the type instance to which the data belongs, so you don't have to worry about the data being affected elsewhere because you modify it somewhere. This is useful in multi-threaded environments, where different threads may change data without your knowledge in multithreaded situations. When this bug occurs, it is very difficult to debug.
Because the appearance difference between a value type and a reference type is that when you modify the data for a type instance, they treat the raw type data differently. However, there is a case where the value type and the reference type are handled similarly, when the data for the type instance is read-only. There is no difference between a value type and a reference type in the absence of a modification.
You may find this useful, if a class is completely undefined, then it is better to conform to some of the habits of NSObject objects that use cocoa and to maintain the original semantics. Today, in Swift you can create a non-redefined class by defining immutable storage properties, which avoids the exposed API being modified. In fact, many classes in common cocoa frameworks, such as Nsurl, are defined as non-redefined classes. Nonetheless, Swift does not currently provide any mechanism to force a class to be a non-redefining (such as a subclass) like struct (struct) and enum (enum).
How do I choose the right type?
If you want to create a new type, should you choose a value type or a reference type? When you use the Cocoa Framework, many APIs are nsobject subclasses, then you have to use the reference type, which is class. In other cases, here are some guidance suggestions you can refer to:
Cases where value types are used:
When comparing instance data using the = = operator.
When you use the = = = operator to determine whether two objects refer to the same object instance.
more about iOS development The knowledge, you can go to mentor network to learn video tutorials.
Explore the value types of swift values and the value of reference types