Introduction to the

A struct is a collection of data of the same or different types that can define properties (storing properties, which can be constants, variables) and add methods.

Structs are value types

In Swift standard library, most public types are structures, such as Bool, Int, Double, String, Array, Dictionary and other common types.

grammar

define

Struct is defined by the keyword struct:

struct nameStruct { 
   def 1
   def 2. method1
   method 2. }Copy the code

Example: Time structure, which defines three storage properties and a method

struct Time {
    var hour: Int
    var minute: Int
    var second: Int
    
    func showTime() {
        print("\(hour):\(minute):\(second)")}}Copy the code

use

All structures have an initializer automatically generated by the compiler. Depending on the situation, the compiler may generate multiple initializers for the structure. The purpose is to ensure that all members have initial values.

When defining a structure, if the attributes in it are not initialized, the attributes that are not initialized are mandatory parameters when initializing the structure.

If hour, minute, and second are not initialized, hour, minute, and second are not initialized. If hour, minute, and second are initialized, hour, minute, and second are initialized.

When willMinute and secondInitialize assignment at definition time, then when calling the structure initializer,hourWill pass,Minute and secondOptional, soTime0, time1, time2Is correct,time3abnormal

The initializer that is automatically generated by the structure is called above, but we can also customize the initializer. Of course, once we have a custom initializer, the compiler will not automatically generate other initializers for it.

Same principle: custom initializers should also be ensuredAll members have initial valuesOtherwise, it is abnormal. Such as:

Custom initializer to ensure hour can be initialized:

struct Time {
    var hour: Int
    var minute: Int = 0
    var second: Int = 0
    
    init(h: Int, m: Int, s: Int) {
        self.hour = h
        self.minute = m
        self.second = s
    }
    
    func showTime() {
        print("\(hour):\(minute):\(second)")
    }
}

var time3 = Time(h: 23, m: 59, s: 59);
Copy the code

The nature of the initializer

struct Time {
    var hour: Int = 23
    var minute: Int = 59
    var second: Int = 59
}

var time = Time()
Copy the code

Is equivalent to:

struct Time {
    var hour: Int
    var minute: Int
    var second: Int
    init() {
        hour = 23
        minute = 59
        second = 59
    }
}

var time = Time()
Copy the code

The assembly code is identical in both cases

The structure contains its internal layout

The actual memory used by the structure is the sum of the bytes of each member of the structure. Of course, the actual allocated memory is worth according to the memory alignment parameter.

struct Time {
    var hour: Int = 23
    var minute: Int = 59
    var second: Int = 59
    
}

print("Actual use:",MemoryLayout<Time>.size)
print("Actual distribution:",MemoryLayout<Time>.stride)
print("Align value:",MemoryLayout<Time>.alignment)
Copy the code

Console print information:

Planned Use: 24 Allocated: 24 Aligned value: 8Copy the code

Struct type is value type:

struct Time {
    var hour: Int = 23
    var minute: Int = 59
    var second: Int = 59
    
}

var time1 = Time()
print("Before assigning time1 - \ (time1. Hour) : \ [time1. Minute) : \ [time1. Second)")
var time2 = time1
time2.minute = 23
time2.second = 23
print("Time1 -\(time1.hour):\(time1.minute):\(time1.second)")
print("Time2 -\(time2.hour):\(time2.minute):\(time2.second)")
Copy the code

Output information:

Time1-23:59:59 before assignment and changed time1-23:59:59 after assignment and changed time2-23:23:23Copy the code

You can see that when you assign time1 directly to time2, modifying time2 does not affect time1.