Swift construction and analysis process

Source: Internet
Author: User


Construction Process
The construction process of SWIFT is implemented by defining the constructor. However, unlike objective-C, the swift constructor does not need to return values, and does not need to indicate func.
In addition, attribute observer is not triggered when the constructor assigns a value to the storage attribute.
Customized Construction Process
1. In addition to the default constructor, we can add custom constructor or covenience initializer for it by adding parameters ). 2. the constructor automatically generates external parameter names. If you do not want them, use the underscore "_" to block them. 3. If the attribute is of the optional type, if no value is assigned to the constructor, the default value of the compiler is nil. 4. As long as the attributes of the constant are determined before the constructor ends, you can modify the attributes of the constant at any point in time during the constructor, but not in the subclass. 5. If init is not written, the default constructor will also be used. 6. struct has a default one-by-one member constructor.

Class inheritance and construction process
Constructor proxy
We can call other constructors to complete Part Of The constructor, and we can only call self. init within the constructor.
Note that all storage-type attributes of the class, including those inherited from the parent class, must be set during the construction process.
Constructor chain
In the apple language document, three principles are described for the constructor proxy call:
1. The specified constructor must call the specified constructor of its direct parent class. 2. the constructor must call other constructor defined in the same class. 3. the constructor ends by calling a specified constructor.
In simple terms, the constructor calls the constructor up, while the constructor calls the constructor horizontally.



Two-stage construction process
Phase 1: each storage type property sets the initialization value by introducing their class constructor. Stage 2: Step 2 customization before preparation.
To ensure the successful completion of the two-stage structure, the compiler will execute four effective security checks:
1. The specified constructor must ensure that all attributes introduced by its class are initialized before other tasks can be forwarded to the parent constructor. 2. to specify the constructor, you must call the constructor of the parent class to add new values to the inherited attributes. 3. the constructor must call other constructor before enabling new values. Otherwise, it may be overwritten. 4. Before the constructor completes the first stage, it cannot call any instance method to access instance attributes and self.

Constructor inheritance and overloading
Swift subclass does not inherit the constructor of the parent class by default. This mechanism prevents a simple constructor of the parent class from being inherited by a more professional subclass, and is mistakenly used to create a subclass instance. We can reload the constructor of the parent class in the Custom subclass. Unlike methods, attributes, and subscript scripts, you do not need to use the override keyword when reloading the init method.
However, when special conditions are met, the constructor of the parent class can be automatically inherited. There are two principles:
1. If the subclass does not define the specified constructor, It inherits the specified constructor of the parent class. 2. If the sub-class provides all parent class specified constructors, the constructor will be automatically inherited.
Specify the constructor and constructor syntax



The initialization process complies with the above principles. However, classes are different from struct and there is no one-by-one initializer.

Set attribute values through closures or functions
Generally, an empty parentheses are added after a code block, indicating that the closure is executed immediately and the return value is assigned to the attribute rather than the closure itself. When the closure initializes the attribute, the other part has not been initialized. This means that other attributes cannot be accessed in the closure, and self is not allowed.
Example: chessboard


Here, the color array of the checkerboard held by the struct uses a closure to create it. But we noticed that the array is written in [bool] instead of the previous bool [], which is the latest revision of swift syntax in the Xcode-beta3 of the current (14.7.11) Apple.

Structure Process
The analysis structure is a concept often mentioned in C ++, similar to dealloc in OC and finalize in Java. When an instance is about to leave the heap memory, we will call the deinit function to perform some extra cleaning.
Automatic Reference count
Reference Loop
In oC, we often use _ weak to modify weakself before calling self in the block to prevent reference loop problems caused by Block Variable capture. This is also the case in swift. However, in addition to weak references, swift adds a non-primary reference. It should be noted that the weak reference must be a variable rather than a constant.
So what is unowned. The unowned keyword is used to modify the content with always-valued values. Note that if an instance is destroyed and accessed without a primary reference, crash is triggered.
In scenarios where there is no primary reference, sometimes we have two attributes pointing to each other and cannot be nil. In this case, we need to mark one attribute as non-primary, and the other as an optional property for implicit parsing (use !).
Example:


We can note that the country attribute is city, and capitalcity points country to itself during initialization, in this case, the country of the city is designed to have no primary reference, while the capital of the country is an implicit optional attribute (!).
Strong circular references caused by closures
When capturing values, especially self, the closure needs to be very careful. In this case, we need to modify the captured instance as unowned when defining the parameter list.



Note that the use of the weak or unowned keyword in the parameter list is related to whether the modified content is nil. If the instance may be nil, do not use unowned.


This blog mainly describes the construction and analysis process of instances in swift. All of the above content is for this blog. You are welcome to discuss it by mistake.

This article and all other articles about SWIFT playground are collected in https://github.com/Rannie/PlaygroundSwift
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.