Swift's Initialization method

Source: Internet
Author: User

Before we dive into the initialization method, let's consider how the initialization in Swift is intended.

is actually safe. In Objective-c, the Init method is very insecure: no one can guarantee that Init is called only once, and no one guarantees that after the initialization of the method invocation, the individual variables of the instance are initialized, and even if the attribute is set in the initialization, it can cause various problems. While Apple has made it clear that attributes should not be accessed in Init, this is not a compiler coercion, so there are many developers who make such mistakes.

So Swift has a super-strict initialization method. On the one hand, 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 super version of designated initialization is also forced (explicitly or implicitly) in the subclass, so whatever path is taken anyway, The initialized object can always complete initialization.

[CPP] view Plaincopy
  1. Class ClassA {
  2. Let Numa:int
  3. Init (num:int) {
  4. NumA = num
  5. }
  6. }
  7. Class Classb:classa {
  8. Let Numb:int
  9. Override Init (Num:int) {
  10. NumB = num + 1
  11. Super.init (Num:num)
  12. }
  13. }

In the example code above, note that in Init we can assign a let instance constant, which is an important feature of the initialization method. Let's declare the value in Swift as a non-variable and cannot be written to the assignment, which is useful for building thread-safe APIs. And because Swift's init can only be called once, in Init we can assign values to invariants without causing any thread-safety problems.

Corresponding to the designated initialization method is the initialization method of adding the convenience keyword before init. 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 cannot be overridden by a class or is called from a subclass in Super mode.

[CPP] view Plaincopy
  1. Class ClassA {
  2. Let Numa:int
  3. Init (num:int) {
  4. NumA = num
  5. }
  6. Convenience init (Bignum:bool) {
  7. Self.init (num:bignum? 10,000:1)
  8. }
  9. }
  10. Class Classb:classa {
  11. Let Numb:int
  12. Override Init (Num:int) {
  13. NumB = num + 1
  14. Super.init (Num:num)
  15. }
  16. }

As long as the Init method required to override the parent class convenience method is implemented in the subclass, we can also use the convenience initialization method of the parent class in the subclass. For example, in the above code, we implemented init (num:int) rewrite in CLASSB. Thus, even if there is no bignum version of convenience init (Bignum:bool) in ClassB, we can still use this method to complete the subclass initialization:

[CPP] view Plaincopy
    1. Let Anobj = ClassB (bignum: true)
    2. Anobj.numa = 10000, Anobj.numb = 10001

So to summarize, you can see that the initialization method always follows the following two principles:

    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 designated initialization method of the subclass must call the designated method of the parent class 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. One of the biggest benefits of this is that the convenience that relies on a designated initialization method can always be used. A ready-made example is the above init (Bignum:bool): If we want this initialization method to be available for subclasses, then Init (num:int) should be declared as necessary so that we call Init (Bignum:bool) in the subclass It is always possible to find a fully initialized path:

[CPP] view Plaincopy
  1. Class ClassA {
  2. Let Numa:int
  3. Required Init (num:int) {
  4. NumA = num
  5. }
  6. Convenience init (Bignum:bool) {
  7. Self.init (num:bignum? 10,000:1)
  8. }
  9. }
  10. Class Classb:classa {
  11. Let Numb:int
  12. Required Init (num:int) {
  13. NumB = num + 1
  14. Super.init (Num:num)
  15. }
  16. }

It is also necessary to note that, in fact, not only the designated initialization method, for the convenience initialization method, we can add required to ensure that subclasses to implement it. This is helpful when you require subclasses to not directly use the convenience initialization method in the parent class.

Swift's Initialization method

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.