Probing: thinking about self = [Super init]

Source: Internet
Author: User


There are two ways to initialize an object: [Class new] and [[Class Alloc] init]

For the latter, there is the process of allocation and initialization, alloc allocates enough memory for the object from the application's virtual address space, and adds a reference count of the new object to 1, initializes the object's member variable to zero, and Init does the real initial work of giving a reasonably useful value to the instance variable of the object.

It is generally not recommended to use [class new], and [[Class Alloc] new] is recommended to view the source code analysis:

+New {     IDNewObject = (*_alloc) ((Class) Self,0); Class Metaclass= self->Isa; if(Class_getversion (Metaclass) >1)     return[NewObject init]; Else     returnNewObject;} //and alloc/init like this:+Alloc {return(*_zonealloc) (Class) Self,0, Malloc_default_zone ()); } -Init {returnSelf ;}

Discovering that [class new] calls Alloc and the Init method by default, we cannot use a custom initialization method with more limitations. [Class Alloc] init] will be more convenient, of course [Class Alloc] init] design is also due to historical reasons.

Why do you write that?
- (instancetype) init{    if (self = [Super init]) {        //  Custom Initialization    }    return self ;}

We know that Alloc returns a valid uninitialized object instance. For self is a pointer returned by Alloc and can be accessed within all method scopes.

But for super, it's just a "compiler indicator" that tells the compiler to search for the implementation of the method in the parent class.

The first call to [Super Init] is to enable the superclass to do their own initialization work.

So what if (self = [super init])?

Here is the fear that the parent class initialization failed, if the initialization of an object fails, will return nil, when the return nil when the body of the self = [super init] test will not continue to execute. If you do not, you may be manipulating an unusable object whose behavior is unpredictable and may eventually cause your program to crash.

Understanding Self & Super

See a classic online topic:

@implementation son:father-(ID) init{    = [Super init];     if (self)    {        NSLog (@ "%@"class]));        NSLog (@ "%@"class]));    }     return Self ;} @end

Self represents the object of the current class, and super is a compiler identifier, and self points to the same message receiver. In this case, either self class or Super class, the recipient is the son object, but the super and self are different, when self calls the class method, it looks for the method in the subclass son, and Super calls the class method. is to find the method in the parent class father.

When the [Self class] method is called, the function is converted to the Objc_msgsend function, which is defined as follows:

ID objc_msgsend (ID self, SEL op, ...)

This is the beginning of the message delivery and forwarding process, will first find the method from the cache, and then the current class, if still find not to go to the parent class, until the NSObject class

For the NSObject class, the-(Class) class is implemented as follows:

-(Class)class  {      return  object_getclass (self);  }

So the print result is son.

When the [Super class] method is called, it is converted to objc_msgsendsuper, and the function is defined as follows:

ID objc_msgsendsuper (struct objc_super *super, SEL op, ...)

Objc_msgsendsuper function The data type of the first parameter super is a struct that points to objc_super

struct objc_super {   ID  receiver;   __unsafe_unretained Class Super_class;};

The struct contains two members, the first is receiver, which represents an instance of the class. The second member is the record of what the parent class of the current class is, and it takes precedence to find the-(class) class from the Father class, and then the process of message passing.

It will be found that either self, or super points to the message recipient is the same, and through the message delivery, the final method of processing the message is the NSObject-(Class) class method.
Reference article:

Objective-c Runtime

http://yulingtianxia.com/blog/2014/11/05/objective-c-runtime/#objc_msgSend%e5%87%bd%e6%95%b0

In-depth understanding of Objective-c's runtime mechanism

http://WWW.CSDN.NET/ARTICLE/2015-07-06/2825133-OBJECTIVE-C-RUNTIME/1

What exactly are super in objective-c?

http://stackoverflow.com/questions/3095360/what-exactly-is-super-in-objective-c

Probing: thinking about self = [Super init]

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.