Swift Learning Notes (v) Construction process

Source: Internet
Author: User

A construction process is a preparation process that uses an instance of a class, struct, or enumeration type. During the construction process, the initial value presets and other necessary preparation and initialization work are performed for each attribute.

The constructor of Swift, compared to OC. No return value is required. At the same time, during the construction of classes and structs, you must have all of the storage type properties, including those inherited from the parent class. Give the appropriate initial value. The storage type value cannot be in an unknown state.

There are two methods for initializing a property, the first: Using the constructor method, and the second: assigning the default value directly when defining the property.

When a property is assigned a value using a construction method, no matter what the property detector is triggered.

When a property always uses the same initial value. It is possible to assign values by default values. This allows the constructor to be more concise, and at the same time be able to voluntarily export the property's type.

In a constructor, you can change a constant property. Principle: The constant value can be determined only before the end of the constructor, and the constants can be modified at random points in the constructor.

The number and type of parameters that can be added to the constructor can be determined according to the detailed needs. However, in each enactment constructor, all properties must be assigned values.

When a type has more than one formulation constructor, the constructor that needs to be called is determined primarily by the name and type of the parameter.

Therefore, the external parameter name of the constructor is particularly important.

Like normal functions, the system proactively generates an external parameter name that is the same as the internal name for each of the constructor's parameters. is equivalent to adding a "#" before the constructor.

Suppose you want to remove such a default structure. The ability to use "_" to define your favorite external parameter names.

Note: The external parameter name must be passed when the constructor is called. Otherwise, the compiler will make an error.

Assume that a property can be empty. When defined, it can be declared as an optional attribute type

such as: Var string:string?


About the default constructor:

When all properties in a struct, enumeration, type have provided a default value and do not provide any constructors themselves, the system will voluntarily generate a constructor itself. The values for all the properties in the constructor are the default values.


About specifying constructors and convenience constructors

To ensure that all stored properties in the class, including those inherited from the parent class, have an initial value.

Swift provides two constructors: specifying constructors and facilitating constructors.

Each class must have at least one development constructor, adding convenience keyword before init. It can be declared as a convenience constructor.

Specifies the invocation relationship between the constructor and the convenience constructor. You must follow these three rules:

1. Specifies that the constructor must call the constructor of its immediate parent class. That is Super.init ()

2. Convenience constructors must call other constructors defined in the same class. Includes the development of constructors and convenience constructors.

3. The convenience constructor must finally end with a call to a set constructor.

Summarize for example the following: Making a constructor is an upward proxy. The convenience builder is a horizontal agent.


about inheritance and overloading;

In the subclass. The constructor of the parent class is not inherited by itself, and it needs to be called manually, super. Init

When you overload the constructor function. Don't write Overridekeyword.

The following two rules apply when you provide default values for introducing discretionary new properties to subclasses:

1. When no constructor is specified in the subclass, the subclass will itself inherit the constructor of all the parent classes.

2. Assume that the subclass provides the implementation of all the parent class-making constructors. This is achieved either through rule 1. or by its own definition, it inherits the convenience constructor of all of the parent classes on its own initiative.


Swift Learning Notes (v) Construction process

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.