Understanding of if (self = [super init]) in Objective-C language

Time:2021-7-24

When learning OC development for the first time, this codeif(self = [super init])I don’t quite understand. The book and the Internet say that if you want to initialize a subclass, you must initialize the parent class, but why? Let’s explore.

Let’s first assume that we have created a class ourselves. We call it MyObject, which inherits from nsobject.
As like as two peas, you know, MyObject is the same as what the NSObject is.
Nsobject is the root of almost all classes in OC. Member variables and functions are added on the basis of nsobject.
The parent class is not your own class. You can’t guarantee that it is safe. You may not know what it looks like if it can be initialized successfully.

Self = [super init], you may have difficulty understanding this step. Self points to the memory address of the parent class initialization. If the initialization function of class fails, it will return nil, and then self will also be nil.
Then if (self = Nil), your next initialization work is for nil, and the initialization fails.
If you want to have a deeper understanding, please see the following paragraph I excerpted for you:
When initializing the parent class, the child object pointer self is used. The init method of the parent class may have made major changes to the memory area pointed to by this pointer (it is possible to release the memory referred to by this pointer and reallocate another memory area to return. At this time, the returned pointer points to a different address from the original pointer), Therefore, the pointer should be returned again after the parent class is initialized. The type of self returned by the parent class and the child class is ID type (common type). If the pointer returned by the init method of the parent class is different from self, release and new memory allocation have been made in the init method of the parent class. You don’t need to release self. Just use the pointer returned by the init method of the parent class.

I hope it can help you understand, but at the same time, I hope you don’t get to the point at the initial stage, but move forward quickly. After accumulating enough experience, it will be easier to understand when you look back

if ( self = [super init] )

{
    Just know what you need to initialize here

}

return self;

Equivalent to if (self = [super init] & & self= NIL)

This is not required in the base class. It is only used in subclasses.[super init] uses the parent class for initialization to see if the parent class can be initialized. If it cannot be initialized, the child class cannot inherit the properties and methods of the parent class。 This subclass is meaningless and cannot have the properties of the parent class. Therefore, if (self = [super init]) is added to this place to judge this. If you cannot inherit, you will directly return an empty object (meaningless).

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]