HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Swift structure is the code used to build a common and flexible structure.
We can define the properties of the structure (constants, variables) and add methods to extend functional structure.
C and Objective C is different:
Structure need not include the implementation file and interfaces.
Structure allows us to create a single file, and the system will automatically generate the code for other external interfaces.
Structure always be replicated by way of transfer in code, so its value can not be modified.
We define the structure through the keyword struct:
struct nameStruct { Definition 1 Definition 2 …… Definition N }
We define a structure called MarkStruct, properties structure for students to score three subjects, the data type Int:
struct MarkStruct{ var mark1: Int var mark2: Int var mark3: Int }
We can come to a structure by structure name.
Examples of the structure using thelet keyword:
import Cocoa struct studentMarks { var mark1 = 100 var mark2 = 78 var mark3 = 98 } let marks = studentMarks() print("Mark1 是 \(marks.mark1)") print("Mark2 是 \(marks.mark2)") print("Mark3 是 \(marks.mark3)")
The above program execution output is:
Mark1 是 100 Mark2 是 78 Mark3 是 98
Example, we 'studentMarks' access to student achievement through the structure name. Structure member is initialized to mark1, mark2, mark3, data type integer.
We then use thelet keyword structure studentMarks () is instantiated and passed to the marks.
Finally, we passedvalue number to access the structure member.
Traditional values and cloning of a structure is instantiated by the following examples of the structure:
import Cocoa struct MarksStruct { var mark: Int init(mark: Int) { self.mark = mark } } var aStruct = MarksStruct(mark: 98) var bStruct = aStruct // aStruct 和 bStruct 是使用相同值的结构体! bStruct.mark = 97 print(aStruct.mark) // 98 print(bStruct.mark) // 97
The above program execution output is:
98 97
In your code, you can define your custom data type structures.
Examples of the structure is always to define your custom data types passed by value.
In accordance with the general rule, when compliance with one or more of the following criteria, consider building structure:
For example, the following situations for the use of the structure:
width
property and the height
property, both of which are Double
type. start
properties and length
properties, both of which are Int
type. x
, y
and z
properties, three are Double
type. Examples of the structure is 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("优异成绩:") var marks = markStruct(mark1: 98, mark2: 96, mark3:100) print(marks.mark1) print(marks.mark2) print(marks.mark3) print("糟糕成绩:") var fail = markStruct(mark1: 34, mark2: 42, mark3: 13) print(fail.mark1) print(fail.mark2) print(fail.mark3)
The above program execution output is:
优异成绩: 98 96 100 糟糕成绩: 34 42 13
The above example, we define the structure markStruct, three members of the property: mark1, mark2 and mark3. Structure in vivo using member properties Use the self keyword.
From the examples we can well understand the structure instance is passed by value.