iOS class initialization

Source: Internet
Author: User

There are three types of initialization classes that inherit:+ (void) Load: + (void) Initialize: -(instancetype) init: + (void) Load: will be automatically invoked (or manually called), as long as the reference runtime will automatically call the class's + (void) Load method, that is, #import "" + (void) Initialize: called automatically (or manually), before the first method of a class is called. It is also stated that runtime's call to + (void) load is not considered the first method of the class. -(instancetype) init: Manual Call requires the Super keyword to invoke the parent class's method, so that the parent class will do the same  runtime call + (void) load is not autorelease pool because the program has not established its Autorelease pool when runtime calls + (void) load, So the code that will need to use the Autorelease pool, there will be exceptions. This is very important to note that the objects placed in the + (void) load should be alloc out and cannot be freed using Autorelease.
+ (void) load + (void) Initialize
Timing of execution 1, the program runs immediately after the execution; 2, the program will be executed only once, 3, if a method only want to execute once, put it here 1, when the method of the class is called for the first time to execute; 2. It is not invoked when the program is started;
If you do not define it, use the parent class method Whether Is
Definitions in the classification All executed, but methods later in the class Overrides a method in a class, performing only one
Based on this feature, when creating a tool class, we can use the+ (void) initialize to complete initialization of some objects  //--------------------------Test--------------------------------------------------------- -define the Gatest class:#import <Foundation/Foundation.h> @interface gatest: nsobject + (void) gatest;
-(void) test; @endimplementation:#import "GATest.h" @implementation gatest -(Instancetype) init{
If(Self= [super init]) {
         nslog (@ " Init ... "    }
    return self;
}
+ (void) load{
    NSLog< Span data-mce-= "" > (@ "load ....  }
+ (void) initialize{
     nslog (@ "Initialize ..." }+ (void) gatest{
NSLog(@ " called the Gatest class method "); }@end--------------------------------------------------------------------------------- ---#import<Foundation/Foundation.h>
#import"GATest.h"
IntMainIntargcConstChar* argv[]) {
@autoreleasepool{
Insert code here ...
NSLog(@ "Hello, world!" );
[Gatest Gatest];
gatest *gat = [[gatest alloc]init];
[Gat Test];
}
return 0; }Results: ------------------------------------------------------------------------------------

iOS class initialization

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.