Swift structure is a general and flexible construct used to build code.
We can define attributes (constants, variables) and add methods for the structure to extend the functionality of the structure.
Unlike C and Objective C, the difference is:
- The structure does not need to include implementation files and interfaces.
- The structure allows us to create a single file, and the system will automatically generate an external interface for other code.
Structures are always passed in the code in a copy way, so their value is not modifiable.
grammar
We define the structure through the keyword struct:
struct nameStruct { Definition 1 Definition 2 …… Definition N }
Example
We define a structure called MarkStruct, the properties of the structure are scores for students' three subjects, and the data type is Int:
struct MarkStruct{ var mark1: Int var mark2: Int var mark3: Int }
We can access structure members through structure names.
Structure instantiation uses the let keyword:
import Cocoa struct studentMarks { var mark1 = 100 var mark2 = 78 var mark3 = 98 } let marks = studentMarks() print("Mark1 yes \(marks.mark1)") print("Mark2 yes \(marks.mark2)") print("Mark3 yes \(marks.mark3)")
The above program execution output results are:
Mark1 is 100
Mark2 is 78
Mark3 is 98
In the example, we access students' grades through the structure name 'studentMarks'. The structure members are initialized to mark1, mark2, mark3, and the data type is integer.
We then instantiate the structure studentMarks() and pass it to marks by using the let keyword.
Finally, we access the value of the structure member through the . number.
The following instantiation passes values when instantiating the structure and clones a structure:
import Cocoa struct MarksStruct { var mark: Int init(mark: Int) { = mark } } var aStruct = MarksStruct(mark: 98) var bStruct = aStruct // aStruct and bStruct are structures that use the same value! = 97 print() // 98 print() // 97
The above program execution output results are:
98
97
Structural Application
In your code, you can use structs to define your custom data type.
Structural instances are always passed through value to define your custom data type.
According to common guidelines, consider building a structure when one or more of the following conditions are met:
- The main purpose of the structure is to encapsulate a small number of relevant simple data values.
- It is reasonable to expect that when an instance of a structure is assigned or passed, the encapsulated data will be copied rather than referenced.
- Any value type attributes stored in the structure will also be copied instead of referenced.
- A structure does not need to inherit another existing type attribute or behavior.
For example, the following situations are suitable for use of structures:
- The size of the geometry, encapsulating a width attribute and a height attribute, both of which are Double type.
- A path within a certain range encapsulates a start attribute and a length attribute, both of which are Int types.
- One point in the three-dimensional coordinate system encapsulates x, y and z attributes, all of which are Double types.
Structural instances are passed by values 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 results:") var marks = markStruct(mark1: 98, mark2: 96, mark3:100) print(marks.mark1) print(marks.mark2) print(marks.mark3) print("Bad results:") 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
Poor results:
34
42
13
In the above example, we define the structure markStruct, with three member properties: mark1, mark2 and mark3. Use the self keyword using member attributes within the structure.
From the instance we can understand well that structure instances are passed through values.
The above is a detailed explanation of the Swift structure. For more information about Swift structure, please follow my other related articles!