define

The Swift structure is a generic and flexible construct for building code.

We can extend the functionality of a 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.

A structure is always passed through the code by being copied, so its value is immutable.

grammar

We define a structure with the keyword struct:

struct nameStruct { 
   Definition 1
   Definition 2
}
Copy the code

For example, we define a MarkStruct structure named MarkStruct. The attribute of the structure is the student’s scores for three subjects, and the data type is Int:

struct MarkStruct{
   var mark1: Int
   var mark2: Int
   var mark3: Int
}
Copy the code

We can access structure members by structure name.

Struct instantiation uses the let keyword:

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 98Copy the code

In our example, we access the student’s score with the structure name ‘studentMarks’. The structure members are initialized to mark1, mark2, mark3, and the data type is integer.

We then instantiate and pass the struct studentMarks() to marks by using the let keyword.

Finally, we access the value of the structure member through the. Sign.

The following instantiations pass values and clone a struct at struct instantiation time:

struct MarksStruct { var mark: Int init(mark: Int) { self.mark = mark } } var aStruct = MarksStruct(mark: 98) var bStruct = aStruct // aStruct and bStruct are structs with the same value! bStruct.mark = 97print(aStruct.mark) // 98
print(bstruct. mark) // 97 The output of the above program is: 98 97Copy the code

Structs apply to your code, and you can use structs to define your custom data types.

Struct instances always define your custom data types by value passing.

As a general guideline, 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 an instance of a structure is assigned or passed, the encapsulated data will be copied rather than referenced. Any value type attributes stored in the structure will also be copied rather than referenced. A structure does not need to inherit the properties or behavior of another existing type. For example, structs are appropriate in the following situations:

The size of the geometry encapsulates a width and height property, both of type Double. A path within a range encapsulates a start attribute and a length attribute, both of type Int. A point in a three-dimensional coordinate system encapsulates x, y, and z attributes, all of which are of type Double. Struct instances are passed by value rather than by reference.

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("Excellent results :")
var marks = markStruct(mark1: 98, mark2: 96, mark3:100)
print(marks.mark1)
print(marks.mark2)
print(marks.mark3)

print("Bad grades :")
var fail = markStruct(mark1: 34, mark2: 42, mark3: 13)
print(fail.mark1)
print(fail.mark2)
print(Fail. Mark3) The output results of the above programs are as follows: Excellent performance: 98 96 100 Poor performance: 34 42 13Copy the code

In the example above we define a markStruct structure with three member attributes: mark1, mark2, and mark3. The structure body uses member attributes using the self keyword.

Struct PWpoint {var X:Int = 0 var Y:Int = 0 var Y:Int = 0name() {print("name---name"} // Inside the structure, add a static before the property or method, Static var Z:Int = 0 static func name(name:String){static func name(name:String){print(name)}} // You need to instantiate the structure to access its methodslet T = PWpoint(X: 2, Y: 3)
T.name()
printPWpoint.Z = 20 PWpoint. Name (name:"pengwei")

Copy the code

Use the init method in the body of the structure to construct the function. If the non-optional property defined is not assigned, an initial value must be given to the non-optional property within the function.

struct markStruct{
    var mark1: Int
    var mark2: Int
    var mark3: Int
    
    init(mark1: Int, mark2: Int){
        self.mark1 = mark1
        self.mark2 = mark2
        mark3 = 0
    }
}
var marks = markStruct(mark1: 98, mark2: 96)
print(marks.mark1)
print(marks.mark2)
print(marks.mark3)


Copy the code