Swift sees the SWIFT structure

Source: Internet
Author: User

struct struct we should not be unfamiliar, we have been learning this since we first came into contact with C language, and in OC, Swift's structure is similar to the structure of C and OC;

When we define a struct, the format is as follows:

struct Rect {    var width:double = 0.0    var height:double = 0.0}

Here we define a rect struct with two variables and an initial value of 0.0, creating an instance of the struct body:

var rect:rect = rect ()

Does it look familiar to you? It's similar to C and OC.

The width and Height methods in this instance of accessing Rect are also in point syntax:

Rect.width = 10rect.height = 10

This allows us to complete the definition of a struct and create a struct-body instance.

If we do not assign an initial value to the variables in the struct when we define the struct, then we use the constructor without any parameters, the system will error, then we can use the constructor with parameters, also known as the function constructor:

var rect:rect = rect (Width:9, Height:9)

Notable things: The parameter order of the function constructor can not be adjusted, the number of parameters can not be increased or decreased.


Member Methods for structs:

Unlike the C and OC structures, Swift's struct can contain member methods, which are behaviors, which are similar to the concept of classes in object-oriented:

We have just added an area to the RECT structure:

struct Rect {    var width:double = 0.0    var height:double = 0.0    func getarea () double{        return Widt H * Height    }}

As we can see, the definition of a method is the same as that of an external global function, so how do you use that member method?

In fact, the calling member variable and the calling member method are the same, all with the dot syntax:

Rect.getarea ()

When we define a rect type, there is another way to define it:

var rect2:rect = Rect1

Rect1 is a type of rect variable that defines RECT2 equals rect1

So Rect2 and Rect1 point to the same block of memory space or pure values?

We modify the value of the rect1 and then print the Rect1 and rect2:

var rect2:rect = Rect1rect1.width = 30println ("rect1.width=\ (Rect1.width), rect1.height=\ (rect1.height)") println (" Rect2.width=\ (Rect2.width), rect2.height=\ (rect2.height) ")

We get the following output:

rect1.width=30.0, rect1.height=20.0rect2.width=20.0, rect2.height=20.0

So we come to the conclusion that

Structs are value types, Rect2 and rect1 are two different instances and are 2 memory spaces.

Swift sees the SWIFT structure

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.