Latest web development tutorials

Swift structure

Swift structure is the code used to build a common and flexible structure.

We can define the properties of the structure (constants, variables) and add methods to extend functional structure.

C and Objective C is different:

  • Structure need not include the implementation file and interfaces.

  • Structure allows us to create a single file, and the system will automatically generate the code for other external interfaces.

Structure always be replicated by way of transfer in code, so its value can not be modified.


We define the structure through the keyword struct:

struct nameStruct { 
   Definition 1
   Definition 2
   Definition N


We define a structure called MarkStruct, properties structure for students to score three subjects, the data type Int:

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

We can come to a structure by structure name.

Examples of the structure using thelet 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 above program execution output is:

Mark1 是 100
Mark2 是 78
Mark3 是 98

Example, we 'studentMarks' access to student achievement through the structure name. Structure member is initialized to mark1, mark2, mark3, data type integer.

We then use thelet keyword structure studentMarks () is instantiated and passed to the marks.

Finally, we passedvalue number to access the structure member.

Traditional values ​​and cloning of a structure is instantiated by the following examples of 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 above program execution output is:


Application structure

In your code, you can define your custom data type structures.

Examples of the structure is always to define your custom data types passed by value.

In accordance with the general rule, when compliance with one or more of the following criteria, consider building structure:

  • The main purpose of the structure is used to encapsulate small amounts of data related to simple values.
  • Reasonable to expect that a structure instance assignment or transfer, the encapsulated data will be copied, not referenced.
  • Any type of property values ​​stored in the structure, it will be copied, not referenced.
  • The structure does not need to inherit property or other types of behavior already exists.

For example, the following situations for the use of the structure:

  • Geometric size and shape of the package a width property and the height property, both of which are Double type.
  • Paths within a certain range, a package start properties and length properties, both of which are Int type.
  • Three-dimensional coordinate system that package x , y and z properties, three are Double type.

Examples of the structure is 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

var marks = markStruct(mark1: 98, mark2: 96, mark3:100)

var fail = markStruct(mark1: 34, mark2: 42, mark3: 13)

The above program execution output is:


The above example, we define the structure markStruct, three members of the property: mark1, mark2 and mark3. Structure in vivo using member properties Use the self keyword.

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