Swift11/90days-the initiation of the egg-ache process

Source: Internet
Author: User

The initial process of egg acheStage Construction

Swift's construction process is divided into two stages:

    • In the first stage, each stored-type property sets the initial value by introducing its own constructor.
    • The second stage is to further customize the storage-type properties before new instances are ready for use.
Security checks

During the construction process, Swift carries out four security checks.

Security 1

Specifies that the constructor must ensure that all properties introduced by its class must be initialized before the other construction tasks can be proxied up to the constructor in the parent class.

For example, the following code is wrong:

class  Food var  init(name:string)  {        Self.name = name    classrecipeingredientfoodvarInit  (name:string, quantity:int){        //error! self.quantity = Quantity    }}
Security 2

Specifies that the constructor must first call the parent class constructor on the proxy, and then set the new value for the inherited property. If this is not done, the new value given by the specified constructor is overwritten by the constructor in the parent class.

For example, the following code is wrong:

class  Food var  init(name:string)  {        Self.name = name    classrecipeingredientfoodoverride  Init(name:string){        "why"//error! super.init (name:name)    } }
Security 3

The convenience constructor must first invoke the other constructors in the same class, and then assign a new value to any property. If this is not done, the new value given by the convenience constructor will be overwritten by the other specified constructors in the same class.

For example, the following code is wrong:

class  Food var  init(name:string)  {        Self.name = name    classrecipeingredientfoodvarInit  (name:string, quantity:int){        self.quantity = quantity        super.init (name:name)     overrideinit(name:string){        2//error! 1)    }}
Security 4

The constructor cannot call any instance method, cannot read the value of any instance property, or reference the value of self until the first stage of construction is complete.

For example, the following code is wrong:

class  Food{varNameString init(name:string) {self.name = name}}class recipeingredient: Food{varQuantityInt init(name:string, quantity:int) {self.quantity =quantity println(self.name) //error! super. Init (name:name) } override convenience init(name:string) {Self.init (name:name, Quantity:1)    }}
The initial process of egg ache

Swift's initialization process is rigorous, and colleagues who have just turned from OC may have written this code in a habitual way:

 class viewcontroller: uiviewcontroller {Private LetAnimator: UidynamicanimatorRequired Init (coderAdecoder: Nscoder) {// ERROR:  Property ' Self.animator '  notInitialized atSuper. Init callSuper. Init (Coder:Adecoder) Animator =Uidynamicanimator(Referenceview:  Self. View)}}

Yes, it's an error! Oh, remember, to initialize your properties in front of super:

  class   Viewcontroller : uiviewcontroller  { private let animator:  uidynamicanimator  required Init (coder adecoder:  nscoder ) {// use of the property  ' view '  in  Base Object before super . Init initializes it animator = uidynamicanimator  (referenceview:  self . View) super . Init (coder:  Adecoder )    }}

Yes, it's an error! Oh, I remember. You cannot call this property before the parent class of the view is initialized. Let's not assign a value to it:

class Viewcontroller Uiviewcontroller Private  Let Uidynamicanimator    Init (coder Adecoder:nscoder)  {        animator = Uidynamicanimator ()        //Error:cannot assign to the result of this expression animator.ref Erenceview = Self.view    }}

No, this property is actually read-only!

We had to set the property to the optional type of let and put the initialization into the viewdidload:

class Viewcontroller Uiviewcontroller Private var  init(coder adecoder:nscoder)  {        super.init (coder:adecoder)    overrideviewdidload(){        Super.viewdidload ()        animator = Uidynamicanimator (ReferenceView:self.view)    }}

At this time animator finally can safely access Self.view this attribute.

You think this is over? Think too much.

Because it is an optional type, you need to unpack it when you use it:

if  Let Actualanimator = animator {  Actualanimator.addbehavior (Uigravitybehavior ())}

This is too ugly, or with a forced unpacking:

animator!. Addbehavior (Uigravitybehavior ())

Or with an optional chain:

Animator?. Addbehavior (Uigravitybehavior ())

are Too! low! The

This is the time to try an implicit parsing of optional types:

class Viewcontroller Uiviewcontroller Private var  init(coder adecoder:nscoder)  {        super.init (coder:adecoder)    overrideviewdidload(){        Super.viewdidload ()        animator = Uidynamicanimator (ReferenceView:self.view)    }}

It seems to look much better. But this is not the right way to open, I think the design of alternative types of engineers should not want us to use this method, after all, it is more like a compromise to solve the legacy of history.

It might be more appropriate to load with lazy delay:

class Viewcontroller Uiviewcontroller {    privatevarreturn uidynamicanimator (referenceView:self.view)        Init  (coder adecoder:nscoder){        super.init (coder:adecoder)    override Viewdidload (){        super.viewdidload ()        Animator.addbehavior (Uigravitybehavior ())    }}
References
    • Initialization
    • Deinitialization
    • SWIFT initialization and the PAIN of Optionals

Swift11/90days-the initiation of the egg-ache process

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.