Overview
The properties related to instances in Swift can be divided into 2 categories
Store Property
- Similar to the concept of member variables
- Stored in the memory of the instance
- Structures and classes can define storage attributes, enumerations cannot define storage attributes
Computed Property
- The essence is the method (function)
- Does not occupy the instance Memory
- Enumerations, structures, and classes can define calculated attributes
Storing attributes
For storing attributes, Swift has clear regulations
When creating classes or structures For example, you must set an appropriate initial value for all storage attributes
- You can set an initial value for the storage attribute in the initializer
- You can assign a default The value of as part of the attribute definition
Calculated attribute
The new value passed in by set is called by default newValue span>, you can also customize
p>
Read-only calculation attributes: only get, no set
You can only use var to define calculated attributes, not let because let stands for constant: the value is 10% Change
Lazy storage attribute
Use lazy to define a lazy storage attribute, which is used for the first time The attributes will be initialized
p>
The lazy attribute must be var, not let because let must have a value before the initialization method of the example is completed
If multiple threads are at the same time the first orientationlazy attribute cannot guarantee that the attribute is initialized only once
Notes on delayed storage attributes
When the structure contains a delayed attribute, only var can access the delayed storage attribute
Because the memory of the structure needs to be changed during the initialization of the delayed storage attribute