I. instance Initialization
Instance Initialization is the process of preparing a class, structure, or enumeration instance for use. Initialization includes setting each storage attribute of an instance as an initial value, and executing any other new instance to use the previously required settings or initialization.
A class, structure, or enumeration can define an initialization method to set its features to ensure that all attributes of its instance have valid initial values.
You can call the class, structure, or enumeration initialization method to execute the instance initialization process.
Class instances can also implement a structure, which is used to execute any specific clearing process before the release of the class instance to release the allocated proprietary resources.
1. Definition of initialization method
The initialization method definition is similar to the definition of an instance method. It can contain parameters or not. You can also specify local and external parameter names for parameters. However, the initialization method uses init and does not return values.
The simplest initialization method is the init method without parameters.
Struct Fahrenheit {
Var temperature: Double
Init (){
Temperature = 32.0
}
}
Var f = Fahrenheit ()
The above defines a structure named Fahrenheit, including an initialization method init, which is used to assign an initial value of 32.0 to its unique storage attribute temperature when the structure is instantiated.
In addition to setting the initial value for the instance attribute in the initialization method, you can also provide a default value for the attribute definition.
Providing default values for attributes during attribute definition is a better way to initialize attributes. On the one hand, the syntax is simpler, and on the other hand, the type of attributes can be inferred based on the type of the provided default values, it also makes better use of the features of default initialization and initialization inheritance.
The same structure definition below is simpler than the above structure definition syntax.
Struct Fahrenheit {
Var temperature = 32.0
}
You can use input parameters and optional attribute types to define one or more custom initialization methods, multiple custom initialization methods are differentiated and called Based on the names and types of parameters provided by each initialization method.
The following example shows how to use parameters to define different initialization methods for a structure, and how to call the corresponding initialization methods according to different names of parameters provided by the initialization method.
Struct Celsius {
Var temperatureInCelsius: Double = 0.0.
Init (fromFahrenheitfahrenheit: Double ){
TemperatureInCelsius = (fahrenheit-32.0)/1.8
}
Init (fromKelvinkelvin: Double ){
TemperatureInCelsius = kelvin-273.15.
}
}
Let boilingPointOfWater = Celsius (fromFahrenheit: 212.0)
// BoilingPointOfWater. temperatureInCelsius is 100.0
Let freezingPointOfWater = Celsius (fromKelvin: 273.15)
Because the parameter names and types of the initialization method play an important role, if you do not provide an external name in the definition of an initialization method, swift automatically provides an automatically assigned name for each of its parameters. The external parameter names automatically generated by Swift are the same as the local parameter names.
It is the same as the instance method. If you do not want to provide an external parameter name for the initialization method parameter, you can use '_' as the external name of the parameter.
The value of a constant attribute can be modified during initialization, but the constant attribute of an instance of the class can only be modified in the initialization method of the class that introduces this attribute, it cannot be modified by its subclass.
2. Default Initialization Method
If a structure or base class does not provide a custom initialization method and all its attributes provide default values, Swift can provide it with a default initialization method, the default initialization method sets the default values of all attributes of the newly created instance to their default values. Example:
Class ShoppingListItem {
Var name: String?
Var quantity = 1
Var purchased = false
}
Var item = ShoppingListItem ()
Because all three attributes of ShoppingListItem have default values, and ShoppingListItem is a base class, it does not provide custom initialization methods, therefore, Swift provides a default initialization method without parameters.
3. schema type member initialization method (memberwise Initializers)
If all the storage properties of a structure have default values and no custom initialization method is defined, you can use the default initialization method provided by Swift, it also automatically receives an initialization method for each member.
One-by-one initialization of members is a shortcut for initializing the member attributes of the new structure instance. The initial values of all attributes of the new instance are transmitted to the initialization method one by name and then set. As follows:
Struct Size {
Var width = 0.0, height = 0.0
}
Let twoByTwo = Size (width: 2.0, height: 2.0)
In this example, the structure Size contains only two storage attributes, width and height, with default values, and does not provide initialization methods, therefore, it automatically receives an init (width: height :) member initialization method one by one. You can use it to initialize a new Size instance. The initial values of its attributes are provided one by the parameters of the member initialization method.
For value types (structure and enumeration), you can use self. init to call other initialization methods of the same type within your own defined initialization methods.
4. class initialization
Because a class involves inheritance, the initialization process of a class is complicated. You need to complete the initialization of all the storage attributes of the class (including any attributes inherited from its super-class chain) during the initialization process. To ensure the correct initialization of a class, Swift defines the initialization methods for the two classes: assigning the initialization method and facilitating the initialization method.
4.1 class assignment initialization method and convenient Initialization Method
The class assignment initialization method is the main class initialization method. A class's assignment initialization method initializes all the attributes introduced by the class itself, and then calls its direct superclass initialization method up to continue the initialization process of the superclass handler. A class must have at least one initialization method. Generally, a class has only one initialization method.
The convenient initialization method of a class is the secondary Initialization Method of the class. You can define a convenient Initialization Method for the class to call other initialization methods of the same class. A class does not have to provide convenient initialization methods.
Convenient initialization method, as a convenient initialization method, is mainly used to create instances of certain instances or classes with specific input values, therefore, when a convenient initialization method is called, some parameters assigned to the initialization method are set to the default value.
The following rules must be observed for calls between class assignment initialization methods and convenient initialization methods:
1) The assignment initialization method must call its direct superclass assignment initialization method;
2) convenient initialization methods must call other available initialization methods of the same class;
3) An assignment initialization method must be called at last to facilitate initialization.
The class assignment initialization method syntax is the same as the simple Initialization Method of the Value Type:
Init (parameters ){
Statements
}
In addition to the convenience keyword, the syntax for convenient initialization methods is the same as that for assigning initialization methods.
Convenience init (parameters ){
Statements
}
4.2 Class Two-Phase Initialization
In Swift, class initialization is completed in two stages. In the first stage, an initial value is assigned to the initialization method of the class where a storage property is introduced. Once the initial status of each storage attribute is determined, the second initialization phase begins: The storage attribute is further customized before the new instance can be used.
To correctly complete the two-phase initialization process of a class, Swift performs the following security checks:
1) assign an initialization method to ensure that all attributes introduced by its class are initialized before calling its direct superclass initialization method.
2) assign an initialization method before assigning a value to an inherited property, you must call its direct superclass initialization method up;
3) the convenient initialization method must call other initialization methods before assigning values to any attribute;
4) An initialization method cannot call any other instance method before the completion of the first stage, read the value of any instance attribute or reference self, because the instance is still in memory uncertainty.
4.3 inheritance and rewriting of initialization Methods
Unlike the Objective-C Language, The subclass in Swift does not inherit its superclass initialization method by default. However, superclass initialization methods can be rewritten in child classes. Unlike methods, attributes, and subscripts, you do not need to write an override identifier to override the initialization method.
4.4 automatic initialization inheritance
In some conditions, the initialization method of a superclass can also be automatically inherited by its subclass.
Rule 1: If a subclass does not define any assignment initialization methods, all assignment initialization methods of its superclass will be automatically inherited. That is, if the initialization method is not inherited, It is inherited.
Rule 2: If a subclass provides the implementation of all assignment initialization methods of its superclasses, it can be inherited by rule 1, or it can provide its own specific implementation, it automatically inherits all convenient initialization methods of its superclass. That is, for convenience initialization method inheritance, if all assignment initialization methods of the super class have been implemented, all convenient initialization methods will be automatically inherited.
5. Set the default value for the property using the closed or function.
You can use a closed or global function to provide specific default values for a storage attribute. When a new instance of the property type is initialized, the closed or global function that provides the default value for a property is called, and a value is returned as the default value of the property. As follows:
ClassSomeClass{
Let someProperty: SomeType = {
Return someValue
}()
}
In this example, a closure is used to provide the default value for the someProperty property. The closure ends with a parentheses, indicating that the closure can be executed.
Note: When you use a closed attribute to assign a default value, because the instance has not been initialized during the closed execution, you cannot access any other attribute values of the instance within the closed window, even if those attributes have their default values, they cannot use the implicit self attribute or call any instance method.
Ii. Analysis Structure
Swift Automatically releases an instance when it is no longer needed. Swift uses automatic reference counting (ARC) to manage the memory of an instance. Therefore, when an instance is released, you do not need to manually clear the system resources it uses. However, when your instance contains and uses the resources you have allocated, you may need to clear some extra work. For example, if you create a class to open a file, you may need to close the file before the instance of the class is released.
Each class can define at least one destructor. The Destructor syntax is as follows:
Deinit {
// Perform the deinitialization
}
The destructor of an instance is automatically called before the instance is released. The super class destructor is inherited by the quilt class. The super class destructor can be automatically called at the end of the subclass's destructor implementation.
The super class destructor are always called, even if a subclass that does not provide its own destructor is not provided.
The instance has not been released before the destructor of an instance. Therefore, all attributes of the instance can be accessed within the destructor.
All rights reserved. Please clearly indicate the link and source when reprinting. Thank you!