The difference between IOS load and initialize

Source: Internet
Author: User
Tags uikit

Maybe some of them are unclear about the difference between load and initialize, so here's a quick look:

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.

When the Initialize method is called, when the first message is sent to the class (typically the class message is first called, the Common is alloc), it is called in the class first, and then in the category (if there is a rewrite in the category), if the class is simply a reference, no call, The Initialize method is not executed.
The common denominator of the two approaches is that the automatic invocation of the parent class does not require a super operation; The automatic call is only called once (excluding external display calls).

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.

The time the load method is called, before the main function, is called in the class, and then in the category (if there is a rewrite in the category).

Code Demo:

#pragram---The code in the main function---#import<UIKit/UIKit.h>#import"AppDelegate.h"intMainintargcChar*argv[]) {NSLog (@"%s", __func__); @autoreleasepool {returnUiapplicationmain (argc, argv, Nil, nsstringfromclass ([appdelegateclass])); }} #pragram---the person class based on nsobject---#import"Person.h"@implementation Person+ (void) load{NSLog (@"%s", __func__);}+ (void) initialize{[Super Initialize]; NSLog (@"%s%@", __func__,[selfclass]);}-(instancetype) init{if(self =[Super Init]) {NSLog (@"%s", __func__); }  returnSelf ;} @end #pragram---the 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__); }  returnSelf ;} @end

Output log:

 .- -- -  the: -:36.535initialize[1572:27457]] +[person Load] .- -- -  the: -:36.535initialize[1572:27457]] +[Girl Load] .- -- -  the: -:36.535initialize[1572:27457]] 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.

#接下来我们来查看一下+ Initialize method, first create the person and Girl object in Viewcontroller:

#import "ViewController.h"#import "Person.h"#import "Son.h"#import "Girl.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; person* P1 = [personNew]; person* P2 = [personNew]; Girl*C1 = [GirlNew]; Girl*C2 = [GirlNew];}@end

Output log:

 .- -- -  the: the:57.134initialize[1840:100060] +[person Load] .- -- -  the: the:57.135initialize[1840:100060] +[Girl Load] .- -- -  the: the:57.136initialize[1840:100060] Main .- -- -  the: the:57.198initialize[1840:100060] +[Person initialize] person .- -- -  the: the:57.198initialize[1840:100060] -[person Init] .- -- -  the: the:57.198initialize[1840:100060] -[person Init] .- -- -  the: the:57.198initialize[1840:100060] +[Girl Initialize] .- -- -  the: the:57.199initialize[1840:100060] -[Girl Init] .- -- -  the: the:57.199initialize[1840:100060]-[girl Init]

+ 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.

so What's the relationship between the parent class and the subclass of initialize? Yang, we create a son class that inherits from the person class:

#pragram code in the---viewcontroller---#import "ViewController.h"#import "Person.h"#import "Son.h"#import "Girl.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; person* P1 = [personNew]; person* P2 = [personNew]; Son*s = [SonNew];}@end

Output log:

 .- -- -  the: -:14.140initialize[1893:109979] +[person Load] .- -- -  the: -:14.142initialize[1893:109979]  +[Son Load] .- -- -  the: -:14.142initialize[1893:109979]  +[Girl Load] .- -- -  the: -:14.142initialize[1893:109979] Main .- -- -  the: -:14.203initialize[1893:109979]  +[Person initialize] person .- -- -  the: -:14.203initialize[1893:109979] -[person Init] .- -- -  the: -:14.203initialize[1893:109979]] -[person Init] .- -- -  the: -:14.204initialize[1893:109979] +[Person Initialize] Son .- -- -  the: -:14.204initialize[1893:109979]-[person Init]

We will find that the + Initialize method of the person class is called again, but look at the child class called by son, that is, when the subclass is created, the subclass will call the parent class's + Initialize method.

This is because when a subclass object is created, the parent class object is first created, so a method of the parent class is called, initialize and when the subclass is created, the initialize method of the parent class is called even though there is no implementation of the method.

Although the initialize method is called only once for a class, there are two classes, so the call is two times compliant, but it does not meet our needs. The correct initialize way to use the posture is as follows

// In person.m+ (void) Initialize {    ifclass]) {        NSLog ( @" Initialize person, caller Class%@ " class ]);    }}

When you add judgment, you don't call your own method because of the subclass initialize .

Summarize:

    1. loadAnd initialize methods are called before the object is instantiated, with the main function as a watershed, which is called before the main function, which is called later. These two methods are automatically called and cannot be called manually.
    2. loadand initialize methods do not show the method of calling the parent class but are called automatically, even if the child class does not have initialize a method that invokes the parent class's method, and the load method does not call the parent class.
    3. loadMethods are commonly used for method Swizzle, which are typically initialize used to initialize global variables or static variables.
    4. loadand initialize methods use locks internally, so they are thread-safe. Keep it as simple as possible, avoid blocking threads, and stop using locks.

The difference between IOS load and initialize

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.