[IOS] Initialize VS load Wars 3k round.

Source: Internet
Author: User

These days and carefully looked at the company's SDK, the previous load, initialize and blurred again, and then ...

YY after still want to do some work to do something ~ t_t ~

+ (void) initialize, + (void) load is the two class method of NSObject, and NSObject as a powerful root class,

Provides a series of life cycle, threading, introspection, runtime, archive, and other methods for other classes. As the Root class has also been

Very capable.

What difference does it have, and how to use it?

1. Load is called whenever a file of the class is referenced, and initialize is called before the first method of the class or its subclasses is called.

So if the class is not referenced into the project, there will be no load call, but even if the class file is referenced but not used, then

Initialize will not be called. There's a lot of crap.

2, +initialize will only be called once, the category of the +initialize method will override the +initialize method of the class itself, and in the category

The +load method does not overwrite the +load method in its own class. If the +initialize method is implemented in the parent class, and the child class does not override this

method, the parent class's method ([Super Initialize]) is used when the subclass is initialized. Of course you don't want to call the initialization method of the parent class, also

Can be rewritten, the code is as follows

+ (void) Initialize {  if (self = = [ClassName self]) {    //...} Do the initialization ...  }
3. Usage Scenarios

The changes to the category are placed in the intialize in the load.


Official explanation:



Verification code and analysis see: http://wufawei.com/2013/06/load-initialize/


If you feel that the above is simple enough, just look at this article.

Http://www.cnblogs.com/ider/archive/2012/09/29/objective_c_load_vs_initialize.html

If you look at the dazzling, English can also look at the foreigner to write this article

Https://mikeash.com/pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html




[IOS] Initialize VS load Wars 3k round.

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.