Objective-c language if (self = [super init])

Source: Internet
Author: User

 We first assume that we have created a class now, and we are named MyObject, and inherit from NSObject. Inheritance know, it is you this subclass (MyObject) If you do not write anything, and the parent class (NSObject) is exactly the same. Almost all of the classes in OC are rooted in nsobject, adding member variables and functions on a nsobject basis. The parent class is not a class you write yourself, you can't guarantee it's safe, and you might not know what it's like in a successful initialization. self = [Super init], you may be difficult to understand this step of the way. Self points to the memory address that the parent class initializes. The initialization function of a class returns nil if it fails, and then self is nil. Then if (self = nil), your downward initialization work is done on nil and initialization fails. If you want to have a deeper understanding, look at the following paragraph: when the parent class initializes, with the subclass object pointer Self, the parent's Init method may make a large change to the memory area that the pointer points to (it is possible to release the memory that the pointer refers to, and reassign another chunk of memory to return, At this point the returned pointer is pointing to a different address than the original pointer, so the parent class is initialized to return the pointer again. The types returned by the parent class and the self of the subclass are of type ID (common type). The Init method of the parent class if the pointer returned is not the same as self, in the parent class Init method has been made release and new memory allocation, do not need to go to release self, directly with the parent class Init method returned by the pointer is possible. if (self = [super init]) {You can}return self as long as you know you need to initialize your work here; 
There is no need for this in the base class, it is only used in subclasses. [Super Init] is initialized with the parent class to see if the parent class can be initialized, and if it cannot be initialized, the subclass cannot inherit the properties and methods of the parent class. This subclass is meaningless and cannot have properties of the parent class. So, in this place plus if (self = [super init]), is used to judge this. If you cannot inherit, you can return empty objects directly (meaningless).

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Objective-c language if (self = [super 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.