9.32. Swift class

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

Page Views: 10 views

The Swift class is a generic and flexible construct used to build code.

We can define properties (constants, variables) and methods for the class.

Unlike other programming languages, Swift does not require you to create separate interfaces and implementation files for custom classes. All you have to do is define a class in a single file, and the system will automatically generate external interfaces for other code.

9.32.1. Comparison of classes and structures #

Classes and structures in Swift have a lot in common. What they have in common is:

  • Define properties to store values

  • Define methods to provide functionality

  • Define satellite scripts to access values

  • Define the constructor used to generate initialization values

  • Extend to add functionality implemented by default

  • Comply with the agreement to provide standard functionality for a class

Compared with structures, classes have the following additional functions:

  • Inheritance allows one class to inherit the characteristics of another class

  • Type conversion allows you to check and interpret the type of an instance ofa class at run time

  • The destructor allows a class instance to release any resources it is assigned

  • Reference counting allows multiple references to a class

9.32.2. Syntax: #

class classname { Definition 1 Definition 2 …… Definition N } 

9.32.3. Class definition #

class student{ var studname: String var mark: Int var mark2: Int } 

Instantiate the class:

let studrecord = student() 

9.32.4. Example #

import Cocoa class MarksStruct { var mark: Int init(mark: Int) { self.mark = mark } } class studentMarks { var mark = 300 } let marks = studentMarks() print("The score is \(marks.mark)") 

The output of the above program execution is as follows:

The score is 300 

9.32.5. Access class properties as a reference type #

The properties of the class can be accessed through the . to visit. Theformat is: instantiate the class name. Attribute name:

import Cocoa class MarksStruct { var mark: Int init(mark: Int) { self.mark = mark } } class studentMarks { var mark1 = 300 var mark2 = 400 var mark3 = 900 } 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 300 Mark2 is 400 Mark3 is 900 

9.32.6. Identity operator #

Because a class is a reference type, it is possible for multiple constants and variables to reference a class instance at the same time in the background.

To determine whether two constants or variables refer to the same class instance, Swift has two identity operators built into it:

Identity operator

Unequal operator

The operator is: =

Operator is:! = =

Returns true if two constants or variables refer to the same class instance

Returns true if two constants or variables refer to different class instances

9.32.7. Example #

import Cocoa class SampleClass: Equatable { let myProperty: String init(s: String) { myProperty = s } } func ==(lhs: SampleClass, rhs: SampleClass) -> Bool { return lhs.myProperty == rhs.myProperty } let spClass1 = SampleClass(s: "Hello") let spClass2 = SampleClass(s: "Hello") if spClass1 === spClass2 {// false print("Referencing the same class instance \(spClass1)") } if spClass1 !== spClass2 {// true print("Referencing different class instances \(spClass2)") } 

The output of the above program execution is as follows:

Referencing different class instances SampleClass 
《地理信息系统原理、技术与方法》  97

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