Swift-04-designated&&convenience

Source: Internet
Author: User

Class ClassA {Let    numa:int    init (num:int) {        NumA = num    }}class classb:classa {let    numb:int    Override Init (num:int) {        NumB = num + 1        super.init (num:num)    }}

We can assign an instance variable to let in Init, which is an important feature of the initialization method. In Swift, the value of the Let declaration is a non-variable and cannot be written to an assignment, which is useful for building thread-safe APIs. And because Init can only be called once, in Init we are able to assign values to invariants without causing any thread-safety problems.

Corresponding to the designated initialization method is the initialization method that is preceded by the Init method with the convenience keyword. Such methods are "second-class citizens" in the swift initialization method and are used only as a supplement and as a convenience. All convenience initialization methods must call the designated initialization completion setting in the same class, and the initialization method of convenience is not overridden by subclasses or is called from a subclass in Super mode.

Class ClassA {Let    numa:int    init (num:int) {        NumA = num    }        convenience init (bignum:bool) {        Self.init (num:bignum 10000:1)    }}class Classb:classa {Let    numb:int    override init (num:int) {        nu MB = num + 1        super.init (num:num)    }}

The convenience method has been added.

As long as the Init method required to override the convenience method of the parent class is implemented in the subclass, we can also use the convenience initialization method of the parent class in the subclass.

Now let's print:

Let Anobj = ClassB (bignum:true) print (Anobj.numa) Let A = Classb.init (num:1) print (A.NUMB)

1. The initialization path must ensure that the object is fully initialized, which can be guaranteed by invoking the designated initialization method of this type;

2. The Designa initialization method of the subclass must call the designated method of the parent class to ensure that the parent class also finishes initialization

For some of the designated initialization methods that we want to be implemented in subclasses, we can force subclasses to override this method by adding the required keyword to restrict it. The benefit is that the convenience that relies on a designated initialization method can always be used. For example, Init (bignum:bool): If we want this initialization method to be available for subclasses, then Init (num:int) is declared as necessary so that when we call Init (Bignum:bool) in the subclass, You can always find a fully initialized path.

  

Also need to explain is: in fact, not only on the designated initialization method, for convenience initialization method, we can also add required to ensure that subclasses to implement it. This is useful in requiring subclasses to not directly use the convenience initialization method in the parent class.

This article, is copy of the Wang Wei predecessors. http://swifter.tips/init-keywords/

Swift-04-designated&&convenience

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.