English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Swift structures
Swift structures are a general and flexible construct used to build code.
We can define properties (constants, variables) and add methods for structures to expand the functionality of structures.
Different from C and Objective C:
Structures do not need to include implementation files and interfaces.
Structures are always passed by value in the code, so their values cannot be modified.
We define a structure by using the keyword struct:
struct nameStruct { Definition 1 Definition 2 …… Definition N }
We define a structure named MarkStruct, with the attributes of the scores of three subjects of the student, the data type being Int:
struct MarkStruct{}} var mark1: Int var mark2: Int var mark3: Int }
We can access structure members through the structure name.
Structure instantiation uses 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 output result of the above program is as follows:
Mark1 is 100 Mark2 is 78 Mark3 is 98
In the example, we access the student's grades through the structure name 'studentMarks'. The structure member is initialized to mark1, mark2, mark3, with data type integer.
Then we use let keyword to instantiate the structure studentMarks() and pass it to marks.
Finally, we use the . number to access the value of the structure member.
The following example demonstrates cloning a structure by passing a value during the instantiation of a structure instance:
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 structures using the same values! bStruct.mark = 97 print(aStruct.mark) // 98 print(bStruct.mark) // 97
The output result of the above program is as follows:
98 97
In your code, you can use structures to define your custom data types.
Structure instances always define your custom data types by value.
According to general guidelines, consider building a structure when one or more of the following conditions are met:
The main purpose of a structure is to encapsulate a small amount of related simple data values.
It is reasonable to expect that a structure instance will encapsulate data that is copied rather than referenced when assigned or passed.
Any value type properties stored in a structure will also be copied, rather than referenced.
Structures do not need to inherit properties or behaviors from another existing type.
For example, the following scenarios are suitable for using structures:
the size of a geometric shape, encapsulating awidth
Properties andheight
Properties, both of which areDouble
Type.
a path within a certain range, encapsulating astart
Properties andlength
Properties, both of which areInt
Type.
a point in a three-dimensional coordinate system, encapsulatingx
,y
andz
Properties, all of which areDouble
Type.
The example of a 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("Get High Score:") var marks = markStruct(mark1: 98, mark2: 96, mark3:100) print(marks.mark1) print(marks.mark2) print(marks.mark3) print("Get Low Score:") var fail = markStruct(mark1: 34, mark2: 42, mark3: 13) print(fail.mark1) print(fail.mark2) print(fail.mark3)
The output result of the above program is as follows:
Get High Score: 98 96 100 Get Low Score: 34 42 13
In the above example, we defined the structure markStruct, with three member properties: mark1, mark2 and mark3. The member properties of the structure are accessed using the self keyword.
From the example, we can understand very well that the structure of the example is passed by value.