Constructor (top)
A construction process is a preparation process that uses an instance of a class, struct, or enumeration type. This process includes setting the initial values for each stored-type property in the instance and performing the necessary preparation and initialization tasks for it.
The construction process is implemented by defining constructors ( Initializers ), which can be seen as special methods for creating instances of a particular type. Unlike constructors in Objective-c, Swift's constructors do not need to return a value, their primary task is to ensure that the new instance completes the correct initialization before the first use.
Classes and structs When an instance is created, the appropriate initial values must be set for all stored-type properties. The value of a stored-type property cannot be in an unknown state.
You can assign an initial value to a stored-type property in the constructor, or you can set a default for a property when you define it. These two methods are described in detail in the following sections.
Note: When you set a default value for a stored-type property or assign a value to it in the constructor, their values are set directly and no property observers are triggered .
Init () { ///This is the simplest constructor and the default constructor does nothing in this constructor, but it usually writes a few constructs in this case }
struct Fahrenheit { var temperature:double// here does not have a given initial value init () { ///temperature is the storage attribute must be given initial value when initializing this instance The initial value can be set during construction temperature = 32.0 }}//called the Fahrenheit constructor temperature was set during construction let Fahrenheit = Fahrenheit () Print (fahrenheit.temperature)//printout: 32.0
Customizing the construction process
In practical development we often customize the construction process, and we can provide parameters for the custom constructor, providing it with the type and name of the value required for the custom construct.
struct Celsius { var temperatureincelsius:double = 0.0 init (Fromfahrenheit fahrenheit:double) { Temperatureincelsius = (fahrenheit-32.0)/1.8 } init (Fromkelvin kelvin:double) { Temperatureincelsius = k elvin-273.15 } //The above declares two constructors that can be selected according to the circumstances to create a new instance}let Boilingpointofwater = Celsius (fromfahrenheit:212.0) Print (Boilingpointofwater.temperatureincelsius)//prints out 100.0let Freezingpointofwater = Celsius (fromkelvin:273.15) Print (Freezingpointofwater.temperatureincelsius)//Printing out 0.0
Note: If the Declaration constructor does not specify an external parameter name,Swift generates an external parameter name that is the same as the internal parameter name to distinguish the different constructors
struct Color { var red:double var yellow:double var blue:double init (red:double, yellow:double, Blue: Double) { self.red = red self.yellow = yellow Self.blue = blue } init (white:double) { red = White yellow = white blue = white } //If you do not want to specify an external parameter name, you can use "_" init (_ black:double) { red = Black yellow = black blue = Black }}//does not specify an external parameter name in the custom constructor, but Swift also defaults to We generated the external parameter name let Redcolor = Color ( red:1.0, yellow:0.0, blue:0.0) Let Whitecolor = Color (white:1.0)//Do not specify external parameter name "init (_ Black:double)" Let Blackcolor = Col or (0.0)
Optional attribute Types
If your custom type contains a stored-type property that logically allows a value to be null-either because it cannot be assigned at initialization time, or because it can be assigned a null value at a later point in time-you need to define it as an optional type. The properties of an optional type are automatically initialized to null nil , indicating that the property was intentionally set to null at initialization time.
Class surveyquestion{ var text:string var response:string? Init (text:string) { Self.text = text ////response is a nullable type, so even if you do not specify a default value during construction, no value is set for it, but it is set to NULL by default (nil) }}
setting constant properties during construction
As long as the value of the constant is determined before the end of the construction process, you can modify the value of the constant property at any point in the construction process.
Note: For a class instance, its constant property can only be modified during the construction of the class in which it is defined, and cannot be modified in a subclass.
Class surveyquestion{let text:string var response:string? Init (text:string) { //text is a constant as long as text does not specify a default value when it is declared, there can be one chance to set its value during construction self.text = text }}
struct-by-constructor
If a struct provides default values for all stored-type properties and does not provide a custom constructor for itself , they can automatically obtain a one-by-member constructor.
struct Size { var width = 0.0 var height = 0.0}//constructor Let size = Size (width:11.0, height:5.0)
Learn Swift--constructors (top)