OC Foundation (vii) Construction method and custom construction method

Source: Internet
Author: User

First, the construction method

The constructor method in OC is the method at the beginning of Init, which initializes an object so that an object is created with some properties and values.

The constructor method belongs to the object method, starting with "-"

For example, the Init method is called when the object is created.

Person *p = [[Person alloc] init];

can be decomposed into

Person *P1 = [Person alloc]; Allocate space

Person *p = [P1 init]; Initializes the object, which is called the Init method

Writing format:

-(Instancetype) init

{

self = [super init];

if (self = nil) {//Here you can synthesize an if (self = [super init]) {

Initialize properties here}

}

return self;

}

Note:

(1) The parent class must be initialized before the subclass is initialized

(2) must determine if the parent class is initialized successfully, only the parent class initialization succeeds to continue initializing subclasses

(3) Returns the address of the current object

OC Foundation (vii) Construction method and custom 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.