Types in swift fall into two categories: first, value types (values types), and each instance of a value type has its own unique data, usually a struct, an enumeration or a tuple, and two, a reference type (reference types), where instances of a reference type share their data, usually a class. In this article we will explore the value of value types and reference types, and how to choose between them.
What's the difference? The most basic feature of a value type is copying data in the process of assigning, initializing, and passing parameters, and creating a separate instance of the data:
Value type example struct S {var data:int =-1}var a = S () var b = a//Copy A to Ba.data = 42//A is changed, B has no println ("\ (a.data), \ (B.data) ")//Prints" 42,-1 "/* Why ask Hovertree.com * *
When copying a reference type, it is implicitly creating a shared instance. After copying, two instances point to the same piece of data, so when one of the instance data is modified, the data for the other instance is also modified, such as:
Example of a reference type class C {var data:int =-1}var x = C () var y = x//x was copied to Yx.data = 42//x pointed data was modified (at the same time Y was also modified) println ("\ (x.data ), \ (y.data) "//Prints" 42, 42 "/* Why ask Hovertree.com * *
The role of variability in security one of the main reasons for choosing a value type instead of a reference type is to make your code easier. If you use a value type in any situation, you can assume that your other code will not make it change, which is often useful in multithreaded environments where the data used in one thread is accidentally modified by another thread, which usually produces very serious bugs and is quite difficult to debug. Because the difference between the two is reflected only when you need to modify the data, the value type and the reference type look exactly the same when your instance does not modify the data. You might think that writing a completely immutable class may be valuable, and using cocoa's nsobject can simplify the process and keep the original semantics well. You can now implement an immutable class in Swift by using immutable storage properties and by avoiding exposing the interface that modifies the data. In fact, most of the cocoa classes, such as Nsurl, are designed as immutable classes, however, Swift does not currently provide any language mechanism to enforce that a class is immutable (such as subclasses can modify the implementation of a Class), and only structs and enumerations are forced immutable. How to choose? So if you want to create a new type, how do you choose? When you write cocoa program, most APIs need to inherit from NSObject, you are already a class (reference type), for other cases, here are some guidelines: Use value type, when ... :
- To compare the data of an instance by using = =
- You want an independent copy of an instance
- Data is modified in a multithreaded environment
Use reference types (such as using a Class) when ... :
- Determine if two instances are identical by using = = =
- You want to create a shared, mutable object
In Swift, Array, String, and dictionary are value types, and their behavior is similar to int in C, where each instance has its own data, and you don't need to do anything extra, such as making an explicit copy to prevent other code from being modified without your knowledge. More importantly, you can safely pass it between threads without the need to use synchronization technology. In the spirit of increased security, this model will help you write more predictable code in swift. Http://www.cnblogs.com/roucheng/p/swiftmianshiti.html
Swift value types and reference types