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

Source: Internet
Author: User

Let's assume that we've created a class today. We are named MyObject and inherit from NSObject.

Inheritance know, it is you this subclass (MyObject) assumes that nothing is written, and the parent class (NSObject) is exactly the same. Almost all of the classes in OC are rooted in nsobject. is to add member variables, functions based on NSObject.


The parent class is not a class you write yourself, and you can't guarantee it's safe. Whether it can be initialized successfully. You may not know what it is like in there. 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 the class assumes that failure will return nil. Then self is nil.


Then if (self = nil), your downward initialization work is done on nil and initialization fails. Suppose you want a deeper understanding. Consider the following paragraph: When the parent class is initialized, the child class object pointer self is used. The Init method of the parent class is likely to 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 again allocate a 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 returns a pointer that is not the same as self. Release and new memory allocations have been made in the Init method of the parent class, and it is not necessary to release self again, and the pointer returned directly with the parent class Init method is available.

if (self = [super init]) {Just know that you need to initialize your work here to}return self;



There is no need for this in the base class. are 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. There's no point in this sub-category. Cannot have properties of the parent class. So, adding if (self = [super init]) in this place is used to infer this. Can not inherit the words. Returns an empty object directly (no meaning).



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.