Swift's convenience && designated Init

Source: Internet
Author: User

Http://www.th7.cn/Program/IOS/201603/789227.shtml

The Init method in OC is very insecure, and no one can guarantee that Init is called only once, and no one is guaranteed to initialize the individual variables of the instance after the initialization method call, or even if the property is set in the initialization, it can cause various problems. Swift reinforces the position of the designated initialization method. The non-decorated Init method in Swift requires that all non-Optional instance variables be initialized in the method, and that the designated initialization of the Super version is also enforced (either explicit or implicit) in subclasses, So no matter how the initialized object can always complete the initialization.

class ClassA {Let       numa:int        ///  unmodified init methods all require that all non-Optional instance variables be initialized     in the method Init (num:int) {         = num        }}class  Classb:classa {let        numb:int         C15>override  Init (num:int) {               1  //  in init we can assign values to let instance constants, This is an important feature of the initialization method. Under normal circumstances, the value of the Let declaration is immutable and cannot be assigned, which is useful for building thread-safe APIs. And Init could only be called once, so in init we can assign values to invariants without causing any thread-safety problems                 super.init (num:num)        }}

The designated initialization method corresponds to the initialization method of adding the convenience keyword before init. Such methods are used only as a supplement and as a convenience in the provision of use. 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 Classaa {Let        numa:int        init (num:int) {                NumA = num        }        convenience init (bignum:bool) {                self . Init (Num:bignum 10,000:1)//All convenience initialization methods must call designated initialization in the same class to finish setting        }}class CLASSBB:CLASSAA {        l ET numb:int        override init (num:int) {                NumB = num + 1                super.init (num:num)        }}

As long as the Init method required to override the parent class convenience method is implemented in the subclass, we can use the convenience initialization method of the parent class in the subclass. For example, if we do not have the bignum version of Convenience init (Bignum:bool) in CLASSBB, we can still use this method to initialize the subclass:

true)
Print (Anobj.numa, anobj.numb)

Outputs: 10000 and 10001

Summary: The initialization method always follows the following two principles

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

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

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 biggest benefit of this is that the convenience that relies on a designated initialization method can always be used.

The following code declares init (Num:int) as mandatory if you want the initialization method to be available for subclasses.

class classaaa {Let        numa:int        required init (num:int) {                = num        }        required Convenience init ( Bignum:bool) {                100001        }}}class  classbbb:classaaa { Let        numb:int        required init (num:int) {                1                super.init (num:num)         true) print (Sencondobj.numa, sencondobj.numb)

Outputs: 10000 and 10001

The initialization method for convenience can also be added required to ensure that subclasses implement it.

Swift's convenience && designated Init

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.