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. We define the structure with the keyword struct: We define a file named We can access structure members through the name of the structure. Instantiation use of structure body The output of the above program execution is as follows: In the example, we access the student’s grades through the structure name And then we use the Finally, we passed. The following instantiation passes a value and clones a structure when it isinstantiated by a struct: The output of the above program execution is as follows: 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 A path within a certain range, encapsulating a A point within a three-dimensional coordinate system, encapsulating attributes of Structure instances are passed by value rather than by reference. The output of the above program execution is as follows: In the above example, we defined the structure From the example, we can well understand that the structure instance is passed by value. 9.31.1. Grammar #
struct nameStruct {
Definition 1
Definition 2
……
Definition N
}
9.31.2. Example #
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
}
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)")
Mark1 is 100
Mark2 is 78
Mark3 is 98
'studentMarks'
. The structural members are initialized as
mark1
,
mark2
,
mark3
, and the data type is integer.
let
keyword sets the structure
studentMarks()
instantiate and pass to the
marks
.
.
to access the value of the structure member.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
98
97
9.31.3. Structural application #
width
properties and
height
property, both are
Double
type.
start
properties and
length
property, both are
Int
type.
x
,
y
, and
z
, all of which are of type
Double
.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)
honour:
98
96
100
Poor grades:
34
42
13
markStruct
with three member properties:
mark1
,
mark2
, and
mark3
. The member properties used within the structure use the
self
keyword.