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, thereby extending the functionality of structures.
Unlike C and Objective-C:
Structures do not need to contain implementation files and interfaces.
Structures allow us to create a single file, and the system automatically generates an external interface for other code.
Structures are always passed in code by being copied, so their values cannot be modified.
Syntax
We define a structure using the keyword `struct`:
struct nameStruct {
Definition 1
Definition 2
……
Definition N
}
Example
We define a structure named `MarkStruct`. The structure's properties are the scores of three student subjects, with a data type of `Int`:
struct MarkStruct{
var mark1: Int
var mark2: Int
var mark3: Int
}
We can access structure members through the structure name.
Structure instantiation uses theletkeyword:
import Cocoa
struct studentMarks {
var mark1 = 100
var mark2 = 78
var mark3 = 98
}
let marks = studentMarks()
print("Mark1 是 \(marks.mark1)")
print("Mark2 是 \(marks.mark2)")
print("Mark3 是 \(marks.mark3)")
The output of the above program execution is:
Mark1 是 100 Mark2 是 78 Mark3 是 98
In the example, we access student scores through the structure name 'studentMarks'. The structure members are initialized as mark1, mark2, mark3, with an integer data type.
Then we use theletkeyword to instantiate the structure `studentMarks()` and pass it to `marks`.
Finally, we use the.dot to access the values of structure members.
The following instantiation passes values during structure instantiation and clones a structure:
import Cocoa
struct MarksStruct {
var mark: Int
init(mark: Int) {
self.mark = mark
}
}
var aStruct = MarksStruct(mark: 98)
var bStruct = aStruct // aStruct 和 bStruct 是使用相同值的结构体!
bStruct.mark = 97
print(aStruct.mark) // 98
print(bStruct.mark) // 97
The output of the above program execution is:
98 97
Structure Applications
In your code, you can use structures to define your custom data types.
Structure instances are always passed by value to define your custom data types.
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 number of related simple data values.
- There is reason to expect that when a structure instance is assigned or passed, the encapsulated data will be copied rather than referenced.
- Any value-type properties stored in a structure will also be copied, rather than referenced.
- Structures do not need to inherit the properties or behavior of another existing type.
For example, structures are suitable in the following situations:
- The size of a geometric shape, encapsulating a
widthproperty andheightproperty, both ofDoubletype. - A path within a certain range, encapsulating a
startproperty andlengthproperty, both ofInttype. - A point in a three-dimensional coordinate system, encapsulating
x,yandzproperties, all three ofDoubletype.
Structure instances are passed by value, not 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("优异成绩:")
var marks = markStruct(mark1: 98, mark2: 96, mark3:100)
print(marks.mark1)
print(marks.mark2)
print(marks.mark3)
print("糟糕成绩:")
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:
优异成绩: 98 96 100 糟糕成绩: 34 42 13
In the above example, we defined the structure `markStruct` with three member properties: mark1, mark2, and mark3. Inside the structure, the `self` keyword is used for member properties.
From the example, we can clearly understand that structure instances are passed by value.
Other Extensions