Swift's structural body

Source: Internet
Author: User

The Swift struct is a common and flexible construct for building code.

We can extend the functionality of a struct by defining attributes (constants, variables) and adding methods for the struct body.

Unlike C and Objective C:

Structs do not need to contain implementation files and interfaces.

Structs allow us to create a single file, and the system automatically generates external interfaces for other code.

The struct is passed in the code in the way it is copied, so its value is not modifiable.


Grammar

We define structs by the keyword struct:

struct Namestruct {

Definition 1

Definition 2

......

Definition N}


Instance

we define a name of The structure of the markstruct, the attribute of the structure is the student's score of three subjects, the data type is INT:


struct markstruct{

var mark1:int

var mark2:int

var mark3:int

}


Struct members can be accessed through struct names.

struct instantiation using the LET keyword:


Import Cocoa


struct Studentmarks {

var mark1 = 100

var MARK2 = 78

var MARK3 = 98

}

Let Marks = Studentmarks ()

Print ("Mark1 is \ (MARKS.MARK1)")

Print ("Mark2 is \ (MARKS.MARK2)")

Print ("Mark3 is \ (MARKS.MARK3)")


The above program execution output results are:


MARK1 is 100.

MARK2 is 78.

MARK3 is 98.


example, we use the struct name ' Studentmarks ' access to students ' grades. Struct members are initialized to Mark1, MARK2, MARK3, and the data type is integer.

We then instantiate the struct Studentmarks () by using the Let keyword and pass it to marks.

Finally, we pass . Number to access the value of the struct member.

The following instantiation passes a value and clones a struct by instantiating the struct:


Import Cocoa


struct Marksstruct {


var mark:int

Init (mark:int) {


Self.mark = Mark

}

}

var astruct = marksstruct (mark:98)

var bstruct = astruct//astruct and bstruct are structs with the same value!

Bstruct.mark = 97

Print (Astruct.mark)//98

Print (Bstruct.mark)//97

Www.fzmajiang.com

The above program execution output results are:


98

97


Application of structural Body

In your code, you can use structs to define your custom data types.

struct instances are always defined by value passing to define your custom data type.

As a general guideline, consider building a struct when one or more of the following conditions are met:

The main purpose of the structure is to encapsulate a small number of related simple data values.

It is reasonable to expect that when an instance of a struct is assigned or passed, the encapsulated data will be copied instead of referenced.

Any value type attribute stored in the struct will also be copied, not referenced.

Structs do not need to inherit the properties or behaviors of another existing type.


For example, the following scenarios are appropriate for the use of structs:

the size of the geometry, which encapsulates a width property and a height property, both of which are double types.

a path within a range that encapsulates a start property and a length property, both of which are of type int.

A point within a three-dimensional coordinate system that encapsulates the X, Y, and Z Properties, all of which are double types.

struct instances are passed by value rather than by reference.


Import Cocoa

struct markstruct{

var mark1:int

var mark2:int

var mark3:int

Init (Mark1:int, Mark2:int, Mark3:int) {

SELF.MARK1 = Mark1

SELF.MARK2 = Mark2

SELF.MARK3 = Mark3

}

}

Print ("Excellent result:")

var marks = markstruct (mark1:98, mark2:96, mark3:100)

Print (MARKS.MARK1)

Print (MARKS.MARK2)

Print (MARKS.MARK3)


Print ("Bad score:")

var fail = markstruct (mark1:34, mark2:42, mark3:13)

Print (FAIL.MARK1)

Print (FAIL.MARK2)

Print (FAIL.MARK3)


The above program execution output results are:

Excellent results :

98

96

100


Bad results:

34

42

13

Www.tiqianzd.com

in the above example we define struct markstruct, three member properties: MARK1, Mark2, and Mark3. The struct body uses the Self keyword with member properties.

From the example we can well understand that the struct instance is passed by value.

Swift's structural body

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.