Swift value type and reference type

Source: Internet
Author: User
Value Type

The value type is assigned to a variable. When a constant or itself is passed to a function, the actual operation is its copy.
In swift, all basic types are as follows:
Integer, floating-point, booleans, String, array, and dictionary are all value types,
It is implemented in the background in the form of struct.
In swift, all struct and enumeration are value types. This means that their instances and any value type attributes contained in the instances will be copied when passed in the code.

Assignment and copying of Collection types

Array and dictionary types in swift are implemented in the form of struct.
However, when an array is assigned a constant or variable, or is passed to a function or method, its copy behavior is somewhat different from that of the dictionary and other struct.
The following describes the copying of arrays, dictionaries, strings, and other values:

In your code, copying does seem to have been generated where there is a copy action. However, in the swift background, only actual (actual) copies are executed if necessary. Swift manages all value copies to ensure optimal performance, so you do not need to avoid assigning values to ensure optimal performance. (The actual assignment is optimized by System Management)

In fact, I don't understand. For the moment, when the array and dictionary are being copied, they reference the type.

Reference Type

When a reference type is assigned to a variable, constant, or passed to a function, it operates on references rather than copies. Therefore, an existing instance is referenced instead of its copy.

let tenEighty = VideoMode()tenEighty.resolution = hdtenEighty.interlaced = truetenEighty.name = "1080i"tenEighty.frameRate = 25.0let alsoTenEighty = tenEightyalsoTenEighty.frameRate = 30.0if tenEighty === alsoTenTighty {    println("tenTighty and alsoTenEighty refer to the same Resolution instance.")}
Note that the difference between "equivalent to" (represented by three equal signs, =) and "equal to" (represented by two equal signs, =) is as follows:
"Equivalent" indicates that constants or variables of two class types reference the same class instance.
"Equal" indicates that the values of the two instances are "equal" or "same". The judgment must comply with the criteria defined by the class designer. Therefore, compared with "equal ", this is a more suitable method.
Class and struct Selection

However, struct instances are always passed through values, and class instances are always passed through references. This means that the two apply to different tasks. When you are considering the data structures and functions of a project, you need to decide whether each data structure is defined as a class or a struct.

According to general rules, consider constructing struct when one or more of the following conditions are met:

  • Struct is mainly used to encapsulate a small amount of related simple data values.
  • It is expected that the encapsulated data will be copied rather than referenced when a struct instance is assigned or transferred.
  • Any value type attribute stored in the struct will also be copied rather than referenced.
  • Struct does not need to inherit another existing type attribute or behavior.

Appropriate struct candidates include:

  • The ry size encapsulateswidthAttributes andheightAttribute, both of which areDoubleType.
  • Encapsulate a path within a certain rangestartAttributes andlengthAttribute, both of which areIntType.
  • A point in the 3D coordinate system, encapsulatedx,yAndzAttribute, all of which areDoubleType.

In all other cases, define a class, generate an instance of it, and manage and transmit it through reference. In practice, this means that most of the custom data structures should be classes rather than struct.

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.