IOS: Those methods for automatic callback/base classes

Source: Internet
Author: User
+ (void) load;This is the method that will be invoked when the application is started (before didfinishlaunchingwithoptions), when load calls, it is not guaranteed that all classes are loaded and available, and that they are responsible for auto release processing when necessary. will only be called once (not the same), equivalent to the program before running all the classes in the program Load method call again, and then call; for a class, no Load method implementation is invoked, no inheritance for NSObject is considered, and the load method for a class does not need to be stated [super Load], the parent class receives the call and precedes the subclass; Initialize calls are not directly triggered. + (void) initialize;

Used to do some initialization work, or a single example mode of implementation. The first time the active use of the current class is invoked; When the Initialize method receives the call, the operating environment is basically sound; The initialize can ensure the thread safety during the operation. Similar to load, subclasses do not implement the Initialize method, and the & inheritance of the parent class is invoked over and over again. The difference is that the parent's methods may have been executed once before, and no super calls are required. (Note: If the subclass implements initialize, or the Initialize method of the parent class has a type judgment for self, you can prevent the parent method from being invoked multiple times)

Note: Initialize and load are the same: the system will be invoked at most once without regard to the active use of the developer; If both the parent class and the subclass are invoked, the invocation of the parent class must precede the subclass, all for the application to run in advance to create the appropriate running environment do not rely heavily on these two methods unless it is really necessary

Reference: http://www.cocoachina.com/ios/20150104/10826.html initwithcoder:

Implement the Nscoding protocol, the class implements the method, and when it is created from storyboard or Xib, the method is recalled; awakefromnib:

Class implements the method, which is recalled when it is created from storyboard or xib;

Note: The last two methods call procedure specific reference Ios:xib (Resource Programming Guide section) && IB New and Alloc/init differences

+ new
{
id newobject = (*_alloc) (Class) self, 0);
Class Metaclass = self->isa;
if (class_getversion (Metaclass) > 1) return
    ;
else return
    newobject;
}
And alloc/init like this:
+ alloc
{return
(*_zonealloc) (Class) Self, 0, malloc_default_zone ());
} -Init
{return
    self;
}

As can be seen above, new is basically equivalent to alloc+ init, the difference is that Alloc allocates memory when the use of zone, this zone is what. When allocating memory to an object, the associated object is allocated to an adjacent memory area, so that it can consume very little cost and improve the processing speed of the program.
And why it is not recommended to use new. If you use new, the initialization method is fixed dead only to invoke Init, and what do you want to call initxx?

+ Allocfromzone: (void *) Z
{return
(*_zonealloc) (Class) self, 0, z);
+ alloc
{return
(*_zonealloc) ((Class) self, 0, malloc_default_zone ());
}

Alloc and init separate the allocated memory from the initialization, and allocate the associated objects to an adjacent area of memory, so that the processing speed of the program is increased by the low cost of the invocation. The new is done with allocating memory and initialization, and can only be initialized with the default Init method, while Alloc can be done in other custom initialization ways.

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.