Ios-method + Initialize and +load (reprint)

Source: Internet
Author: User
Tags uikit

Objective-c has two magical methods: +load and +initialize, which are called automatically when the class is used. However, the differences in the two methods can lead to significant differences in performance at the application level.

First, + Initialize method and +load call timing
  • Let's start with the + Initialize method: Apple officially has a description of this method: This method is called before the class is initialized for the first time , and we use it to initialize the static variable.
    • The Load method is called when the class is loaded, that is, when the iOS app starts, all classes are loaded, and the + Load method for each class is called.
  • Then we combine the code to explore the Initialize and + load two method call timing, first of all is+ Load
    #pragram ---main函数中的代码---
    #import <UIKit/UIKit.h> #import "AppDelegate.h" int main (int argc, char * argv[]) {  NSLog (@ "%s", __func__);  @autoreleasepool {      return Uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Appdelegate class]);  }}
     #pragram ---基于NSObject的Person类--- 
     #import "Person.h" @implementation person + (void) load{NSLog (@ "%s", __func__);}     + (void) initialize{[super Initialize]; NSLog (@ "%s%@", __func__,[self class]); }-(Instancetype) init{if (self = [super init]) {NSLog (@ "%s", __func__);} return to self;} @end 
     #pragram--- Son class based on person---
    #import "Girl.h" @implementation Girl + (void) load{      NSLog (@ "%s", __func__);} + (void) initialize{    [Super Initialize];    NSLog (@ "%s", __func__);} -(instancetype) init{    if (self = [super init]) {    NSLog (@ "%s", __func__);}  return self; } @end
    To run the program, let's look at the output log:
    2015-10-27 15:21:07.545 initialize[11637:334237] +[person load]2015-10-27 15:21:07.546 initialize[11637:334237] +[ Girl Load] 2015-10-27 15:21:07.546 initialize[11637:334237] Main
    This means that when I do not do anything with the class, the +load method is executed by default and is executed before the main function.
  • Let's take a look at+ Initializemethod, first create the person and Girl objects in Viewcontroller:
    #import "ViewController.h" #import "Person.h" #import "Son.h" #import "Girl.h" @interface Viewcontroller () @ End@implementation viewcontroller-(void) viewdidload {  [super viewdidload];  person * a = [person new];  Person * b = [person new];  Girl *c = [Girl new];  Girl *d = [Girl new];} @end
    Let's take a look at the output log:
    2015-10-27 15:33:56.195 initialize[11711:342410] +[person load]2015-10-27 15:33:56.196 initialize[11711:342410] +[ Girl Load] 2015-10-27 15:33:56.197 initialize[11711:342410] main2015-10-27 15:33:56.259 initialize[11711:342410] +[ Person Initialize] person2015-10-27 15:33:56.259 initialize[11711:342410]-[person init]2015-10-27 15:33:56.259 INITIALIZE[11711:342410]-[person init]2015-10-27 15:33:56.259 initialize[11711:342410] +[Girl Initialize] 2015-10-27 15:33:56.260 initialize[11711:342410]-[girl init]2015-10-27 15:33:56.260 initialize[11711:342410]-[Girl init]
    With this experiment we can determine two points:
      • + Initialize method is similar to a lazy load, if you do not use this class, then the system will not call this method by default, and the default is only loaded once;
      • + Initialize call occurs before the +init method.
  • Let's look at the next+ InitializeIn the relationship between the parent class and the child class, create a son class that inherits from the person class:
    #pragram ---ViewController 中的代码---
    #import "ViewController.h" #import "Person.h" #import "Son.h" #import "Girl.h" @interface Viewcontroller () @ End@implementation viewcontroller-(void) viewdidload {  [super viewdidload];  person * a = [person new];  Person * b = [person new];  Son*z = [Son new];} @end
    Check out the output log:
    2015-10-27 15:44:55.762 initialize[12024:351576] +[person load]2015-10-27 15:44:55.764 initialize[12024:351576] +[Son Load]2015-10-27 15:44:55.764 initialize[12024:351576] +[girl load] 2015-10-27 15:44:55.764 initialize[12024:351576] Main2015-10-27 15:44:55.825 initialize[12024:351576] +[person Initialize] person2015-10-27 15:44:55.825 initialize[ 12024:351576]-[person init]2015-10-27 15:44:55.825 initialize[12024:351576]-[person init]2015-10-27 15:44:55.826 INITIALIZE[12024:351576] +[person Initialize] son2015-10-27 15:44:55.826 initialize[12024:351576]-[Person init]
    We will find that the person class+ Initializemethod is called, but it is called by the subclass son, that is, when the subclass is created, the subclass calls the parent class's+ InitializeMethod.
Second, summary
    • If you implement the + load method, it will be called automatically when the class is loaded. This call is very early. If you implement a + loadfor an app or frame, and your app is linked to this frame, then + load is called before the main () function. If you implement + loadin a loadable bundle, it will be called during bundle loading.
    • The call to the + initialize method looks more reasonable, and usually it is better to write code in it than in + load . + Initialize is interesting because it is lazy to call, or it may not be called at all. Class is loaded for the first time,
    • + Initialize will not be called. When a class receives a message, the runtime checks whether + Initialize has been called. If not, it is called before the message is processed.

Ios-method + Initialize and +load (reprint)

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.