iOS Development--swift & Classic Syntax (12) value types and reference types

Source: Internet
Author: User

Value types and reference types in Swift

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) is a value type. 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 a class is a 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.

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 Examplestruct S { Var Data: Int = -1 }Var A = S()Var B = A //a copy and assigns the copy to Badata  = 42//A's data changed, but B didn't change. println ( "\ (a.data), \ (b.data)" )  //prints "1"            

The copy behavior of a 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 only shared instance, and when any of the two variables is changed, the data in the original type is also applied:

Reference type examplesClass C { Var Data: Int = -1 }Var X = C()Var Y = X Assign X to YX.data  = 42//modified the data of X, actually modified the reference data, then Y's data will also change println ( "\ (X.data ), \ (y.data) ")  //prints "", "          
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 value types and reference types are handled in a similar way, when the data for a type instance is read-only. There is no difference between a value type and a reference type in the absence of a modification.

You might find this useful, and if one class is completely undefined, then it is better to conform to the habits of the cocoa NSObject object 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 of the classes in the common cocoa framework, for example NSURL , are defined as non-redefined classes. Nonetheless, Swift does not currently provide any mechanism like structs (struct) and enumerations (enum) to force a class class to be non-redefined (such as a subclass).

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 NSObject of the APIs are subclasses, then you have to use reference types, that is class . In other cases, here are some guidance suggestions you can refer to:

Cases where value types are used:

    • ==when using an operator to compare instance data.
    • When you want to copy an instance data separately.
    • When working with data in a multithreaded environment.

Use a reference type (for example class ) for the case:

    • When using an === operator to determine whether two objects refer to the same object instance.
    • When the context needs to create a shared, mutable object.

In Swift,, Array , String Dictionary are value types. They are used in the same way as in C int , where each instance has a piece of data. You do not need to perform a copy of the display to prevent the data from being modified without your knowledge. What's more, you can pass arguments across threads without having to worry about synchronization, because it's safe to pass a value type . With a high level of security in mind, this type of partitioning model can help you write more predictable code in swift.

iOS Development--swift & Classic Syntax (12) value types and reference types

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.