IOS-+ Initialize and +load

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---The code in the main function---#import<UIKit/UIKit.h>#import"AppDelegate.h"int main (int argc,char * argv[]) {NSLog (@ "%s", __func__);@autoreleasepool {ReturnUiapplicationmain (argc, argv,NilNsstringfromclass ([Appdelegate class])); }}#pragram---The person class based on nsobject---#import"Person.h"@implementationperson+ (void) load{NSLog (@ "%s", __func__);} + (void) initialize{[Super Initialize];NSLog (@ "%s%@", __func__,[Self class]);} -(Instancetype) init{if (Self = [Super Init]) {NSLog (@ "%s", __func__); }ReturnSelf;}@end #pragram---person-based son class--- #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 Span class= "Hljs-number" >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-2715:33:56.195 initialize[11711:342410] +[person Load]2015-10-2715:33:56.196 initialize[11711:342410] +[girl Load]2015-10-2715:33:56.197 initialize[11711:342410] Main2015-10-2715:33:56.259 initialize[11711:342410] +[person Initialize] Person2015-10-2715:33:56.259 initialize[11711:342410]-[person Init]2015-10-2715:33:56.259 initialize[11711:342410]-[person Init]2015-10-27 15:33:56.259 Initialize[11711:342410] +[girl Initialize] 2015-< Span class= "Hljs-number" >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-2715:44:55.762 initialize[12024:351576] +[person Load]2015-10-2715:44:55.764 initialize[12024:351576] +[son Load]2015-10-2715:44:55.764 initialize[12024:351576] +[girl Load]2015-10-2715:44:55.764 initialize[12024:351576] Main2015-10-2715:44:55.825 initialize[12024:351576] +[person Initialize] Person2015-10-2715: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:< Span class= "Hljs-number" >55.826 initialize[12024:351576] +[ Person Initialize] Son2015-10-27 Span class= "Hljs-number" >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-+ Initialize and +load

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.