9.31. Swift structure

发布时间 : 2025-10-25 13:33:48 UTC      

Page Views: 10 views

The Swift structure is a general and flexible construct used to build code.

We can extend the function of the structure by defining properties (constants, variables) and adding methods for the structure.

Unlike C and Objective C,

  • The structure does not need to contain implementation files and interfaces.

  • The structure allows us to create a single file, and the system automatically generates external interfaces for other code.

The structure is always passed in the code by being copied, so its value is immutable.

9.31.1. Grammar #

We define the structure with the keyword struct:

struct nameStruct { Definition 1 Definition 2 …… Definition N } 

9.31.2. Example #

We define a file named MarkStruct the attribute of the structure is thestudent’s score for 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 the name of the structure.

Instantiation use of structure body let keywords:

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 of the above program execution 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 structural members are initialized as mark1 , mark2 , mark3 , and the data type is integer.

And then we use the let keyword sets the structure studentMarks() instantiate and pass to the marks .

Finally, we passed. . to access the value of the structure member.

The following instantiation passes a value and clones a structure when it isinstantiated by a 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 structures that uses the same value! bStruct.mark = 97 print(aStruct.mark) // 98 print(bStruct.mark) // 97 

The output of the above program execution is as follows:

98 97 

9.31.3. Structural application #

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

Structure instances always define your custom data types through value passing.

According to general guidelines, consider building structures when one or more of the following conditions are met:

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

  • It is reasonable to expect that when a structure instance is assigned or passed, the encapsulated data will be copied rather than referenced.

  • Any value type attribute stored in the structure will also be copied rather than referenced.

  • The structure does not need to inherit the property or behavior of another existing type.

For example, structures are appropriate in the following situations:

  • The size of the geometry, encapsulating a width properties and height property, both are Double type.

  • A path within a certain range, encapsulating a start properties and length property, both are Int type.

  • A point within a three-dimensional coordinate system, encapsulating attributes of x , y , and z , all of which are of type Double .

Structure 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("honour:") var marks = markStruct(mark1: 98, mark2: 96, mark3:100) print(marks.mark1) print(marks.mark2) print(marks.mark3) print("Poor grades:") var fail = markStruct(mark1: 34, mark2: 42, mark3: 13) print(fail.mark1) print(fail.mark2) print(fail.mark3) 

The output of the above program execution is as follows:

honour: 98 96 100 Poor grades: 34 42 13 

In the above example, we defined the structure markStruct with three member properties: mark1 , mark2 , and mark3 . The member properties used within the structure use the self keyword.

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

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。