Swift -- (6) Value Type in swift

Source: Internet
Author: User

Tag: swift Value Type

In swift, the struct and enumeration types are value types (structures and enumerations). In stack overflow, there is a problem:The variable attribute in the struct can be modified by the built-in method only after the mutating keyword is used.(If it is an immutable attribute, it cannot be modified, so you don't have to worry about it. But in swift, there is another exception, that isConstructor Initialization,You can set the constant instance attributes of struct and classes in the constructor.This is a bit contrary to the meaning of the constant attribute. You can also think about it carefully. Because the constructor is called at the end of the instantiation, after the constructor, instance attribute value ).

@ Author: retain this line if twlkyao is reprinted or referenced.

Here, we will sort out the reasons. What's wrong? We hope to point out that there are two concepts to be explained before we can solve this problem.

First, whether a value can be modified is not about its type (class or struct), but its storage type (constant or variable ). Only variables can be modified.

Then, the value type can be understood as that each attribute has its own space in the memory, and there is no relationship between the property and other instances, the value type can be understood as a continuous code block. Every time you create a value type instance, it is equivalent to copying such a code block, each copy has its own attribute value. If the instance type is variable, each attribute in this code block can be changed. If an instance type is unchangeable, every attribute in this code block is also unchangeable,Each instance type must support variable attributes and immutable attributes. To meet this condition, swift divides the struct method into two types, which can modify the struct structure and cannot modify the struct structure, the method for modifying the struct structure must be called by a mutable struct instance. Methods without modifying the struct structure can be called by a mutable or immutable struct instance. In most cases, the latter is used, so Apple may directly use the latter as the default situation. The structure method cannot modify the attributes of the struct instance..The reference type can be understood as a pointer (although Swift does not have a pointer, just like Java, but the object-oriented language actually treats class instances as pointers ), point to the same location in the memory space. Each time you create a reference type instance, an extra Pointer Points to this memory address.

Okay. Here we can solve this problem, because the value type instance can be assigned a valueVariableOrConstantThe assigned constant or variable determines whether the value type instance is variable, and then determines whether the value type instance attribute is variable, it can be understood that there are two modes for value-type instance attributes: variable and immutable (Determined by the type of the attribute and the constant or variable assigned by the instance.), The following are the rules:

Instance property variability rules
Instance type (type on the left of the Value Pair) Instance property type Variability
VaR Let Let
VaR VaR VaR
Let Let Let
Let VaR Let


Example:

struct Point {    var x = 0    let y = 0}var a = Point()let b = Point()a.x = 1 // var, right.//a.y = 2 // let, compile time error.//b.x = 3 // let, compile time error.//b.y = 4 // let, compile time error.

As shown above, only when the instance property is a variable and the final instance is assigned a variable can the attribute of the corresponding variable be modified. When the reference type is instantiated, no corresponding property replication is performed, but a pointer pointing to the corresponding property is added, and the property may point to another type of pointer, so the VaR type attribute is also the let type attribute,Are you sure this "Pointer" is the corresponding link can be changed?.

It can be understood that, before the struct is instantiated, the struct does not know whether it is variable. To prevent accidental modification, the struct is immutable by default, unless declared in advance, this is the role of the "mutating" keyword.

The following code describes the value type.

struct Point1 {    var x = 0, y = 0    mutating func moveToX(x: Int, andY y:Int) { // need to be a mutating method in order to work        self.x = x        self.y = y    }}var p1 = Point1(x: 1, y: 2) // in order to change the properties, you have to use var, since it is a value type.p1.x = 3 // works from outside the struct.p1.moveToX(5, andY: 5)println("p1.x = \(p1.x), p1.y = \(p1.y)")/***************************/struct Point2 {    let x = 0, y = 0}var p2 = Point2(x: 1, y: 2)println("p2.x=\(p2.x), p2.y=\(p2.y)")//p2.x = 3 // can‘t change p2.x, since p2.x is a constant.

The following code describes the reference type:

class Point3 {    var x = 0    var y = 0    let plet: Point4    var pvar: Point4    init(x: Int, y: Int) {        self.x = x        self.y = y        self.plet = Point4() // plet.x = 0, plet.y = 0        self.pvar = Point4() // pvar.x = 0, pvar.y = 0    }    func moveToX(x: Int, andY y: Int) { // no need to use "mutating" keyword.        self.x = x;        self.y = y;    }}class Point4 {    var x = 0    var y = 0}let p3 = Point3(x:1, y:2) // you can use let, even though you want to change the property, because it is a reference.p3.x = 2p3.moveToX(5, andY: 5) // no need to use the "mutating" keyword.println("p3.x = \(p3.x), p3.y = \(p3.y)") // x = 5, y = 5var p4 = p3 // p3 and p4 are the same, since they are reference type.p4.x = 3println("p4.x = \(p4.x), p4.y = \(p4.y)") // p4.x = 3, p4.y = 5println("p3.x = \(p3.x), p3.y = \(p3.y)") // p3.x = 3, p3.y = 5/**********************/p3.plet.x = 4println("p4.p.x = \(p3.plet.x), p4.p.y = \(p3.plet.y)") // p3.plet.x = 4, p3.plet.y = 0let p5  = Point4()//p3.plet = p5 // can‘t assign new value to p3.plet since the realtion can‘t change since the p property of p3 is a constant.p3.pvar = p5 // even p3 is a constant, its propery can change.

In the above Code, the most important thing is the code below "*", which can describe the function of let and VaR in the reference type.

For more details, see: http://stackoverflow.com/questions/24035648/swift-and-mutating-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.