Swift structure


Release date:2023-12-01 Update date:2023-12-08 Editor:admin View counts:116

Label:

Swift structure

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.

Grammar

We define the structure with the keyword struct:

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

Example

We define a file named 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
}

We can access structure members through the name of the structure.

Instantiation use of structure body 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)")

The output of the above program execution is as follows:

Mark1 is 100
Mark2 is 78
Mark3 is 98

In the example, we access the student’s grades through the structure name 'studentMarks' . The structural members are initialized as mark1 , mark2 , mark3 , and the data type is integer.

And then we use the let keyword sets the structure studentMarks() instantiate and pass to the marks .

Finally, we passed. . to access the value of the structure member.

The following instantiation passes a value and clones a structure when it isinstantiated by a struct:

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

The output of the above program execution is as follows:

98
97

Structural 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.

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 width properties and height property, both are Double type.

  • A path within a certain range, encapsulating a start properties and length property, both are Int type.

  • A point within a three-dimensional coordinate system, encapsulating attributes of x , y , and z , all of which are of type Double .

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("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)

The output of the above program execution is as follows:

honour:
98
96
100
Poor grades:
34
42
13

In the above example, we defined the structure markStruct with three member properties: mark1 , mark2 , and mark3 . The member properties used within the structure use the self keyword.

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

Powered by TorCMS (https://github.com/bukun/TorCMS).