Basic concept of 22-oc construction method

Source: Internet
Author: User

Construction method

The method that starts with Init in OC, which we call the construction method

Use of construction methods

The purpose of a construction method: to initialize an object so that an object is created with some properties and values

How to implement a construction method

Overriding the Init method, initializing member variables in the Init method

Overriding the Init method

Overriding the Init method must be rewritten in the format specified by Apple, and if it does not follow the rules, it will cause some unknown errors

The parent class must be initialized before the child class is initialized

The parent class must be judged to be successful, only the parent class initialization succeeds to continue initializing the subclass

Returns the address of the current object

-(Instancetype) init

{

1. Initializing the parent class

As long as the parent class initializes successfully, the corresponding address is returned and nil is returned if the initialization fails

Nil = = 0 = = False = not initialized successfully

self = [super init];

2. Determine if the parent class is initialized successfully

if (self! = nil) {

3. Initializing subclasses

Set the value of a property

_age = 6;

}

4. Return address

returnself;

}

Note that you must use super to call the parent class method in the subclass construction method

Custom Construction Methods

Custom construction method is to customize an Init method

Sometimes we need to get some properties of an object when we create an object, and then we need to pass in some parameters to the object's properties, in order to satisfy this requirement, we need to customize the construction method

Format of custom construction methods

Must be an object method

Must return to Id/instancetype

Method name must begin with Init

-(Instancetype) Initwithage: (int) age;

The representation of custom construction methods in inheritance

Own things to do by themselves, whose attributes are to be manipulated by WHO

The properties of the parent class are given to the parent class's methods to handle, and the subclass's methods handle the subclasses ' own unique properties

The custom constructor method in the subclass, how to call the parent class to construct the method's

Subclasses typically use super to invoke the constructor of a parent class when overriding a custom construction method, and first let the parent class initialize the properties of the parent class.

-(Instancetype) Initwithage: (int) Age Andname: (NSString *) name Andno: (int) No

{

if (self = [super Initwithage:age Andname:name]) {

_no = no;

}

returnself;

}

Basic concept of 22-oc construction method

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.