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. 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 Instantiate the class: The output of the above program execution is as follows: The properties of the class can be accessed through the The output of the above program execution is as follows: 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 The output of the above program execution is as follows: Principles, Technologies, and Methods of Geographic Information Systems 102 In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress. 9.32.1. Comparison of classes and structures #
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
}
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 score is 300
9.32.5. Access class properties as a reference type #
.
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)")
Mark1 is 300
Mark2 is 400
Mark3 is 900
9.32.6. Identity operator #
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)")
}
Referencing different class instances SampleClass