Swift structure

Swift structures are a common and flexible construct used to build code.

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

Unlike C and Objective C:

  • Structures do 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.

Structures are always passed through code by copying, so their values are not modifiable.

Grammar

We define structures by keyword struct:

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

Instance

We define a structure called MarkStruct, which has properties that are the scores of the student's three subjects and the data type is Int:

struct MarkStruct{
   var mark1: Int
   var mark2: Int
   var mark3: Int
}

We can access structure members by their names.

Structure instantiation uses the let keyword:

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 an example, we access a student's grades by the structural name 'studentMarks'. S tructure members are initialized to mark1, mark2, mark3, and the data type is integer.

Then we instantiate the structure studentMarks() and pass it to marks by using the let keyword.

Finally, we use . number to access the values of the members of the structure.

The following instantiation passes values and clones a structure as it is instantiated by the 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 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.

In accordance with common 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.
  • 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 properties stored in the structure will also be copied, not referenced.
  • Structures do not need to inherit properties or behaviors of another existing type.

For example, structures are appropriate in the following situations:

  • The size of the geometry encapsulating width height and a height property, both Double types.
  • A range of paths encapsulating start length property, both of which are Int types.
  • A point within the 3D coordinate system encapsulating x y z properties, all of which Double types.

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("优异成绩:")
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. T he self keyword is used in the body using member properties.

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