"We all love Paul Hegarty." Stanford IOS8 public class personal note 6 init

Source: Internet
Author: User

This is the first thing to say about Init.


Initialization is not often used, because most of the properties in the class and struct are initialized by assignment, or some properties are optional, so even nil does not matter, you can assign them later, like the outlet in storyboard, Or you can use closures to initialize, or use lazy to avoid init, so there are many ways to avoid init, unless you really need an init, what do you do?

In some cases, init is automatically generated, in which case you automatically get an initialization method without parameters when all the properties in the class have initial values. struct, it will default to a method of initializing all attributes as arguments, only if there is no initialization method in the struct, as in the example, an mystruct struct can be initialized with an assignment in parentheses.


So what can the initialization method do? First we can reset the default value in the initialization method, such as the default value is 3, and the initialization method assigns it to 4, then its value becomes 4. Even if your property is a let-defined constant, you can still assign it to a value in the Init method. In your classes and structs you can call other initialization methods, using Self.init, so you can invoke other initialization methods with different parameters. Of course you can call Super.init in the class, but there are many rules when calling the Init method of the parent class.


So what do you have to do at Init? First, at the completion of any Init method, you must ensure that all properties are initialized, and note that if there is a optional its value is nil, it is counted as a value.

In the class Swift provides two methods to initialize, note that not in the struct but in the class, one is convenience Initializer, the other is another init, called designated initializers,designated Initializers is the default initialization method. A designated Initializer can only invoke designated Initializer in its parent class, which is a very important rule, if there is a designated Initializer, It is not preceded by the word convenience, it must be called in Init of the parent class and cannot call its own other init, and the init in the parent class must also be of type designated.

In addition, you must first initialize all of your own properties when invoking the initialization method of the parent class!

You have to assign a parent class to its properties before you can assign them a value!

Convenience initializer has different characteristics, it must and can only invoke designated initializer in this class, it cannot invoke any of the parent class's initialization methods, it can be passed through other convenience Initializer to call designated initializer indirectly.

Convenience initializer must call designated initializer directly or indirectly before other values can be accessed.

Finally, the methods and properties in the calling class must be completed before initialization is complete.

Let's talk about inherited initialization.


If you do not implement any designated initializer in your class, then you will inherit all designated initializers from your parent class, otherwise you will not inherit any designated initializer from your parent class.

If you rewrite all the designated initializer, then you will inherit all the convenience initializer in the parent class.

If you add the required keyword before the initialization method, the subclass of the class must implement its initialization method.


So what about Failable init? There are some initialization methods that allow failure and return nil, and the definition of init followed by one? , look at the previous few words to everyone? Should be familiar, the example shows a uiimage, which can get a picture from the project by the name of the picture, but it will return nil if the image is not in the project. Usually in the face of this failable init, we use the if let structure, and if the initialization succeeds we execute the action, otherwise we do the other work. In swift we seldom use try and catch in Java, and the appearance of Try-catch becomes not necessary.


With so much knowledge of initialization, how do you create a new object? The practice is to add a pair of parentheses after the object name you want to create, with the parameters of the initialization method inside the parentheses. But you don't always use this method, and sometimes you use a class method or a type method to create it. As shown in the example, when we created a button, we used a method of this type, UIButton.

There is also a less-used initialization method, sometimes some of your objects will help you to create other objects, such as a very cool method in string is called Join,join accept a string array and separated by the way of join, so the above example returns you a by "," Separating the strings, splitting the myarray into two parts, which is obviously creating a new array.


"We all love Paul Hegarty." Stanford IOS8 public class personal note 6 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.